在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;
}
答案 0 :(得分:1)
我发现在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;
}
答案 1 :(得分:0)
通常,在UITableViewCell的initWithStyle:reuseIdentifier:
方法中分配了reuseIdentifier,您没有使用该方法,因为您正在从Nib加载视图。
您之后无法设置此属性,因为它是只读的。
也许您可以尝试使用标准initWithStyle:reuseIdentifier:
对单元格进行实例化,并将Nib中的视图添加为单元格ContentView的子视图......
现在您的情况是,每次Table View需要显示一个新单元格时,您会创建一个新单元格。显然,这不会起作用。实际上,如果您重复使用单元格,则还必须将文本字段的内容存储在某处(最好是在数据源中),并在重复使用单元格时将其放入。如果你不存储它,当要重用单元格时,它将包含显示它的前一行的数据。