【问题标题】:Selecting cell when selecting the custom UITextView inside the cell选择单元格内的自定义 UITextView 时选择单元格
【发布时间】:2011-04-01 22:25:49
【问题描述】:

我创建了一个自定义单元格函数,其中包含两个 uitextfields,因为我不喜欢 cell.textLabel 和 cell.detailTextLabel 的外观。当我在 cellForRowAtIndexPath 中为表格创建单元格时,将调用此函数。当用户选择单元格时,会弹出一个键盘,因此您可以编辑第一个文本字段。但是,当您触摸或靠近第一个文本字段的文本时,它会拉起键盘,但它不会触发我拥有的 textFieldShouldReturn 函数,如果在其他地方选择了单元格,它就会触发。我有 textFieldShouldReturn 函数将数据保存到应用程序。

这是自定义单元格函数

- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier

{ CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 8, 1, 25);
CGRect Label2Frame = CGRectMake(10, 34, 100, 13);
CGRect Image1Frame = CGRectMake(265, 10, 30, 30);
CGRect Image2Frame = CGRectMake(230, 10, 30, 30);

UITextField *lblTemp;
UIButton *imgTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

//initialize label with tag 1
lblTemp = [[UITextField alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp sizeToFit];
[lblTemp release];

lblTemp = [[UITextField alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
[cell.contentView addSubview:lblTemp];
[lblTemp release];

imgTemp = [[UIButton alloc] initWithFrame:Image1Frame];
imgTemp.tag = 3;
[cell.contentView addSubview:imgTemp];
[imgTemp release];

imgTemp = [[UIButton alloc] initWithFrame:Image2Frame];
imgTemp.tag = 4;
[cell.contentView addSubview:imgTemp];
[imgTemp release];


return cell;

}

cellForRowAtIndexPath 函数

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{
UITableViewCell *cell;  
UITextField *tempValue;  
UITextField *descript;  

UIButton *primaryBtn;

UIButton *secondaryBtn;

cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentity];

if ([valueFromServer isEqualToString:@"comments"]) 
{
    cell = [self cellIsView:cellIdentity];

    messageTextView = (UITextView *)[cell viewWithTag:5];
    messageTextView.returnKeyType = UIReturnKeyDone;
}
if (cell == nil)
{
    cell = [self getCellContentView:cellIdentity];
}

primaryBtn = (UIButton *)[cell viewWithTag:3];
secondaryBtn = (UIButton *)[cell viewWithTag:4];

tempValue = (UITextField *)[cell viewWithTag:1];
if ([valueFromServer isEqualToString:@""])
{
    tempValue.textColor = [UIColor grayColor];
}
tempValue.textColor = [UIColor blackColor];
tempValue.font = [UIFont boldSystemFontOfSize:16];
tempValue.placeholder = @"Touch cell to edit"; 
tempValue.text = getServerData;

descript = (UITextField *)[cell viewWithTag:2];
descript.enabled = NO;
descript.textColor = [UIColor darkGrayColor];
descript.font = [UIFont boldSystemFontOfSize:10];
descript.text = getServerLabel;

[tempValue sizeToFit];

... 

return cell;

}

我使用 willSelectRowAtIndexPath 来触发我的应用程序的其余部分,正如我之前所说,只要未选择标签 tempValue,一切正常。有没有办法,如果用户按下 tempValue 标签,它会将第一响应者传递给单元格,以便正确触发我的应用程序的其余部分?

【问题讨论】:

  • 如果 Stack Overflow 上已经有答案,请将该信息转发给我。

标签: iphone objective-c


【解决方案1】:

我找到了答案,我应该在发布这个问题之前看起来更好。 This answer worked for me 这是 terra 关于 userinteractionenabled 的回答。我所做的是在创建单元格时将我的文本字段的 userinteractionenabled 设置为 NO,在示例代码中将其标记为 tempValue,然后在 willSelectRowForIndexPath 中将 userinteractionenabled 切换为 YES。这太容易了,我应该知道的更好。

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 2012-07-10
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多