在UITableView中进行cell 的重用时,偶尔会出现重影的现象
通常我们这样写:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stee = @"hello";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stee];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:stee];
cell.accessoryType = UITableViewCellAccessoryNone;
}
//标题
TitleL=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 60, cell.contentView.frame.size.height)];
[cell.contentView addSubview:TitleL];
TitleL.text=@"xxx";
}
出现重影之后我们可以这样写:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stee = @"hello";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stee];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:stee];
cell.accessoryType = UITableViewCellAccessoryNone;
//标题
TitleL=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 60, cell.contentView.frame.size.height)];
TitleL.tag=101;
[cell.contentView addSubview:TitleL];
}
UILabel *titleL=(UILabel *)[cell.contentView viewWithTag:101];
titleL.text=@"xxxx";<span style="font-family: Arial, Helvetica, sans-serif;">}</span>
本文详细探讨了在UITableView中使用cell重用机制时遇到的重影问题,并提供了有效的解决方法。通过引入自定义视图的tag属性,避免了cell绘制过程中的重叠显示,确保了用户界面的清晰性和一致性。
1286

被折叠的 条评论
为什么被折叠?



