所以我试图在创建自定义单元格时存储单元格的索引路径
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我在名为“自定义单元格”的类中定义了一个自定义单元格,并向该类添加了一个属性,定义如下:
@property (nonatomic, strong) NSIndexPath *indexPath;
在cellForRowAtIndexPath
我设置索引路径customCell.indexPath = indexPath.
当我NSLog
在下一行时,它返回 null。有人可以解释为什么会这样吗?
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EditableCell";
EditableCell *editableCell = (EditableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (editableCell == nil) {
editableCell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_myTextField = [editableCell textFieldCell];
if (indexPath.section == 0){
[_myTextField setPlaceholder:@"text"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else if (indexPath.section == 1){
[_myTextField setPlaceholder:@"text"];
[_myTextField setReturnKeyType:UIReturnKeyNext];
}
else {
[_myTextField setPlaceholder:@"text"];
[_myTextField setReturnKeyType:UIReturnKeyDone];
}
_myTextField.keyboardType = UIKeyboardTypeDefault;
_myTextField.delegate = self;
return editableCell;
}
我在哪里调用 [self.tableview indexpathforcell:cell]
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.returnKeyType == UIReturnKeyNext) {
UITableViewCell* myCell = (UITableViewCell*)textField.superview;
EditableCell *currentCell = (EditableCell *)myCell;
NSLog(@"INDEXPATH OF CURRENT CELL: %@",[self.tableView indexPathForCell:currentCell]);
}
}