【发布时间】:2013-11-18 18:17:33
【问题描述】:
现在我有一个这样构建的 tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PhotoCell";
PhotoSummaryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *photoCaption = [[[self.form valueForKey:@"photos"] objectAtIndex:indexPath.row] objectForKey:@"caption" ];
cell.textfieldCaption.text = photoCaption;
cell.textfieldCaption.tag=indexPath.row;
return cell;
}
我想要做的是获取已编辑文本字段的索引?我连接了自定义单元格中的文本字段,并在结束编辑时执行了一个操作:
- (IBAction)getIndexForCaption:(id)sender {
[[[self.form valueForKey:@"photos"] objectAtIndex:cell.textfieldCaption.tag] setObject:cell.textfieldCaption.text forKey:@"caption"];
}
但是现在情况如何,我没有“单元格”,所以我收到未声明的标识符“单元格”错误。在这种情况下如何获取自定义单元格?
【问题讨论】:
-
假设
sender是单元格中的UITextField,从文本字段向上走superview,直到获得表格视图单元格。 -
stackoverflow.com/questions/18743099/… 的答案显示了查找“封闭”表格视图单元格的不同方法。
-
仅供参考 - 小心使用
indexPath.row作为标签。这仅在表格的行无法插入(除了末尾之外)、删除或重新排序时才有效。
标签: ios objective-c