在 UITableView 上滚动时,我的单元格文本字段值出现问题。当我向下滚动并隐藏自定义单元格时,textField 的值将被删除。 dequeueReusableCellWithIdentifier 方法不起作用。我有这个:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SectionsTableIdentifier = @"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
cell = [objects objectAtIndex:0];
}
cell.labelCustomAttribute.text= @"Attribute Name";
cell.textFieldCustomAttribute.delegate = self;
return cell;
}
最佳答案
我发现在 viewDidLoad 方法中使用 tableView 注册自定义单元格更容易,然后只需使用 dequeueReusableCellWithIdentifier。如果您注册单元格,dequeue 方法将自动选择一个可重用单元格或分配一个新的自定义单元格(如果没有可用单元格)。
例子:
-(void)viewDidLoad
{
[super viewDidLoad];
// Get a point to the customized table view cell for MyCustomCell
UINib *myCustomCellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
// Register the MyCustomCell with tableview
[[self tableView] registerNib:myCustomCellNib forCellReuseIdentifier:@"MyCustomCell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SectionsTableIdentifier = @"MyCustomCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
cell.labelCustomAttribute.text= @"Attribute Name";
cell.textFieldCustomAttribute.delegate = self;
return cell;
}
关于ios - 具有文本字段删除值的自定义单元格中 dequeueReusableCellWithIdentifier 的 iPhone 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691189/