• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - dequeueReusableCellWithIdentifier、自定义 UITableViewCell 的问题

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:41:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有带有 2 个按钮的自定义单元格(这些按钮的功能只是禁用被按下的按钮)。 当我以这种经典方式使用 dequeueReusableCellWithIdentifier 时:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = ((MainCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]);
 if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed"MainCell" owner:self options:nil];
 }
    return cell;
}

UITableView 有 1 个部分,问题是:在第一个单元格上,当我按下按钮以禁用它,然后向下滚动以显示其他单元格时,当我再次向上滚动时,第一个单元格是一个新单元格,按钮是启用。 我知道如果已经创建了reuseIdentifier,则不会重新创建单元格,但是这样我就丢失了所有不可见的单元格的信息。

有什么想法吗?

提前致谢



Best Answer-推荐答案


我遇到了类似的问题——我认为问题在于在任何给定时间只有可见单元格实际上在内存中,当它重新显示一个旧单元格时,它实际上只是将一个新单元格出列。我认为解决方案是使用 - (void)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath 委托(delegate)方法:

- (void)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath {
    id objectForCell = [self.arrayOfThingsForTableView objectAtIndex:indexPath.row];
    if (!objectForCell.button1IsEnabled) { 
        cell.button1.enabled = NO; //or something along those lines
    } else {
        cell.button1.enabled = YES; //necessary so that all the other buttons don't disable
    }
}

如果有人有更好的解决方案,我会很高兴听到。

关于iphone - dequeueReusableCellWithIdentifier、自定义 UITableViewCell 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493598/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap