我正在尝试使用以下代码在我的UITableViewCells的特定行上显示一个'padlock‘图标:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"];
GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
cell.textLabel.text= topic.name;
if ((indexPath.row == 5) || (indexPath.row == 9))
{
cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];;
[cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
}
return cell;
}我得到了一个有趣的结果--挂锁最初显示在第5,9行,但当我向上和向下滚动列表时,图标也随机地重新显示在其他单元格的accessoryView上(顺便说一下,只有1个部分),滚动变得非常抖动和滞后……向上/向下滚动的次数越多,显示的实例就越多!为什么?这里的bug在哪里?
帮帮忙,谢谢!
发布于 2013-03-12 07:13:55
细胞会被重复使用。您每次都需要重置accessoryView。这只是一个小小的改变:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"];
GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
cell.textLabel.text= topic.name;
if ((indexPath.row == 5) || (indexPath.row == 9))
{
cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
} else {
cell.accessoryView = nil;
}
return cell;
}为了完成,你也可以像这样使用Eric的解决方案-它可能会更快,因为imageView不是每次都创建的。但这只是最小的区别。也许你的滞后还有其他原因。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if(indexPath.row == 5 || indexPath.row == 9) {
cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"];
cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];;
[cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"];
}
GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
cell.textLabel.text= topic.name;
return cell;
}发布于 2020-02-26 12:00:55
在Swift 4和Swift 5中显示自定义accessoryView单元格
let lockIcon = UIImage(named: "lock_icon")
let lockIconView = UIImageView(image: lockIcon)
lockIconView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
cell.accessoryView = lockIconView发布于 2013-03-12 07:16:18
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(indexPath.row == 5 || indexPath.row == 9) {
cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"];
}
GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row];
cell.textLabel.text= topic.name;
if ((indexPath.row == 5) || (indexPath.row == 9))
{
cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];;
[cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)];
}
return cell;
}https://stackoverflow.com/questions/15349983
复制相似问题