【发布时间】: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