我有一个自定义UITableViewCell
,我正在为数据源分配一些属性,例如标签。但我也在cellForRowAtIndexPath:
内动态创建一个控件,然后将其添加到单元格中。由于我使用dequeReusableCellWithIdentifier:
,动态内容会搞砸。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.stepperView.tag = [indexPath row];
[cell.stepperView addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
[[cell.calendarView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
//THIS LINE ADDS A DYNAMIC CONTENT TO THE CELL
[self addDatePicker:cell];
return cell;
}
-(void) addDatePicker:(AutomaticAnnualIncreasesCell *)cell
{
DatePicker *datePicker = [[DatePicker alloc] initWithFrame:CGRectMake(0, 0, cell.calendarView.frame.size.width, cell.calendarView.frame.size.height)];
datePicker.tag = 100;
if([cell.calendarView viewWithTag:datePicker.tag] != nil)
{
[cell.calendarView addSubview:datePicker];
}
}
答案 0 :(得分:0)
你实现它的方式最终会得到addDatePicker:
中添加的任何内容的许多副本。
我会为每个动态内容值的自定义单元格添加一个属性,并在setSomeDynamicProperty:
方法中删除任何现有的子视图并添加新的子视图(或只是调整您拥有的子视图)。这将阻止您获取多个内容副本。如果给定的行不需要,请确保将属性设置为nil
中的tableView:cellForRowAtIndexPath:
。