Ios UITableViewCell在单元格可见时重新添加自定义内容
我在我的Ios UITableViewCell在单元格可见时重新添加自定义内容,ios,uitableview,Ios,Uitableview,我在我的单元格中添加了一个自定义按钮。contentView,我注意到每当单元格从屏幕的可见部分滚动到屏幕上时,按钮就会被重新添加—它的半透明部分变得越来越坚实。正确的处理方法是什么,以便在滚动浏览tableView时不会在顶部堆积更多的对象?请注意,每个单元格的自定义内容不同,因此我无法将其放入if(cell==nil){…}块中 我的密码是: UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArra
单元格中添加了一个自定义按钮。contentView
,我注意到每当单元格从屏幕的可见部分滚动到屏幕上时,按钮就会被重新添加—它的半透明部分变得越来越坚实。正确的处理方法是什么,以便在滚动浏览tableView时不会在顶部堆积更多的对象?请注意,每个单元格的自定义内容不同,因此我无法将其放入if(cell==nil){…}
块中
我的密码是:
UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:btn_title]];
// set various other properties of btn
...
[cell.contentView addSubview:btn];
每次单元出列时,必须先删除旧的子视图,然后再添加新的子视图,否则会产生堆叠效果。您可以在以下两个位置之一执行此操作: a) 在
tableView:cellForRowAtIndexPath:
中,在调用dequeueReusableCellWithIdentifier:
之后和添加新视图之前,删除旧视图
b) 如果使用的是
UITableViewCell
的子类,则可以覆盖prepareforeuse
以删除不需要的视图<每次单元出列以供重用时都会调用code>prepareForReuse,因此这是一个摆脱上次配置单元时的旧视图的好地方。每次单元出列时,您必须在添加新的子视图之前删除旧的子视图,否则会得到堆叠效果。您可以在以下两个位置之一执行此操作:
a) 在tableView:cellForRowAtIndexPath:
中,在调用dequeueReusableCellWithIdentifier:
之后和添加新视图之前,删除旧视图
b) 如果使用的是
UITableViewCell
的子类,则可以覆盖prepareforeuse
以删除不需要的视图<每次单元退出队列以便重用时,都会调用code>prepareForReuse,因此这是一个摆脱上次配置单元时的旧视图的好地方。我将为您发布的代码发布一个修复示例。它可以扩展以处理更多视图
这些步骤是:
setupWithItems:
)cellForRowAtIndexPath:
中有一个单元格(在将其退出队列或创建后),应调用setupWithItems:
,并显示单元格应显示的新项目列表setupWithItems:
实现中,确保从其父视图中删除UISegmentedControl。如果分段控件存储为自定义单元格的属性,则可以轻松地执行此操作setupWithItems:
实现中,创建一个新的UISegmentedControl并将其添加到CustomCell的视图层次结构中-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:kSomeIdentifier];
if (!cell)
{
// Create a new cell
}
NSArray* currentCellItems = [self cellItemsForRow:indexPath.row];
[cell setupWithItems:currentCellItems];
return cell;
}
在CustomCell子类中:
- (void)setupWithItems:(NSArray*)items
{
if (self.segmentedControl)
{
[self.segmentedControl removeFromSuperView];
self.segmentedControl = nil;
}
// More setup...
UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:btn_title]];
// set various other properties of btn
[cell.contentView addSubview:btn];
}
我将为您发布的代码发布一个修复示例。它可以扩展以处理更多视图 这些步骤是:
setupWithItems:
)cellForRowAtIndexPath:
中有一个单元格(在将其退出队列或创建后),应调用setupWithItems:
,并显示单元格应显示的新项目列表setupWithItems:
实现中,确保从其父视图中删除UISegmentedControl。如果分段控件存储为自定义单元格的属性,则可以轻松地执行此操作setupWithItems:
实现中,创建一个新的UISegmentedControl并将其添加到CustomCell的视图层次结构中-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:kSomeIdentifier];
if (!cell)
{
// Create a new cell
}
NSArray* currentCellItems = [self cellItemsForRow:indexPath.row];
[cell setupWithItems:currentCellItems];
return cell;
}
在CustomCell子类中:
- (void)setupWithItems:(NSArray*)items
{
if (self.segmentedControl)
{
[self.segmentedControl removeFromSuperView];
self.segmentedControl = nil;
}
// More setup...
UISegmentedControl *btn = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:btn_title]];
// set various other properties of btn
[cell.contentView addSubview:btn];
}
这可能会做到,但我没有对表单元格进行子类化,因此对于我的目的来说有点太多。这可能会做到,但我没有对表单元格进行子类化,因此对于我的目的来说有点太多。我使用方法a:在运行
dequeueReusableCellWithIdentifier
和if(cell==nil){…}
I dofor(UIView*v in cell.contentView.subview){if([v iskindof class:[UISegmentedControl class]]){[v removeFromSuperview];}}
也可以根据需要添加其他类型的视图。我使用了方法a:在运行dequeueReusableCellWithIdentifier
和if之后(cell==nil){…}
I dofor(UIView*v in cell.contentView.subview){if([v iskindof class:[UISegmentedControl class]]){[v removeFromSuperview];}}}
也可以根据需要添加其他类型的视图。