Right now I have a tableview that is built like this:
- (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;
}
What I want to do is get the index of the textfield that was edited? I wired up the textfield in the custom cell and made an action that executes on ending an edit:
- (IBAction)getIndexForCaption:(id)sender {
[[[self.form valueForKey:@"photos"] objectAtIndex:cell.textfieldCaption.tag] setObject:cell.textfieldCaption.text forKey:@"caption"];
}
But how this is now, I dont have the "cell" so I get undeclared identifier "cell" error. How can I get the custom cell in this case?
There are several options available to do this. Personally, I would prefer make a subclass of UITextField with an extra property for the PhotoSummaryCell
. You could set this property when the cell gets recycled, and this would make getting the index of the text field as easy as:
NSIndexPath *indexOfTextField = [self.tableView indexPathForCell:textField.cell];
Then of course, you could step through the text fields view hierarchy until you find a cell, or save the index path's row into the textfields tag
property as @rmaddy and @Martin R pointed out.
Make sure that you declare your cell
property as weak
to avoid retain cycle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.