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

ios - 如何在一个 uitableview 中为不同的单元格样式分配不同的标识符?

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

我是 ios 开发的大一新生,我想在一个 uitableview 的不同部分实现不同的单元格样式。我的表格 View 有两个部分,每个部分有三行。

我知道不同的单元格应该有不同的重用标识符来检索重用单元格,但是当我编译我的应用程序时,控制台总是显示以下信息:

2012-08-26 14:04:45.571 Defferent Cell Styles[703:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

任何帮助将不胜感激,我的代码如下:

@end

@implement LGViewController

@synthesize data = _data;
@synthesize list = _list;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *dataArray = [[NSArray alloc] initWithObjects"fwfwf", @"ffgfg", @"sfsfsf", nil];
    NSArray *listArray = [[NSArray alloc] initWithObjects"fdff", @"ffdfsw", @"eergerg", nil];
    self.data = dataArray;
    self.list = listArray;
    self.navigationItem.title = @"Data";
}

#pragma mark - DataSource

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return 2;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    if (section == 0) {
        return [_data count];
    }
    else if (section == 1){
        return [_list count];
    }
    return section;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *accessoryIdentifier = @"Cells";
    static NSString *switchIdenfier = @"Cells";
    static NSString *sliderIdentifier = @"Cells";

    UITableViewCell *cell;   
    if ((indexPath.section == 0 && indexPath.section == 1) && indexPath.row == 0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:accessoryIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:accessoryIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }
    else if ((indexPath.section == 0 && indexPath.section ==1) && indexPath.row == 1)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:switchIdenfier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:switchIdenfier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *soundSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [soundSwitch addTarget:self actionselector(showAlert) forControlEvents:UIControlEventValueChanged];
        [soundSwitch setOn:NO animated:NO];
        cell.accessoryView = soundSwitch;


    }
    else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:sliderIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sliderIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        cell.accessoryView = slider;


    }

    return cell;   
}



Best Answer-推荐答案


您永远不会返回单元格,因为 indexPath.section 永远不能同时为 0 和 1:

else if ((indexPath.section ==0 && indexPath.section == 1) && indexPath.row == 2)

应该是

else if ((indexPath.section ==0 || indexPath.section == 1) && indexPath.row == 2)

在所有情况下。 || 是 OR,&& 是 AND。

虽然真的,你可以放弃检查部分,因为它总是 0 或 1。

关于ios - 如何在一个 uitableview 中为不同的单元格样式分配不同的标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12127974/

回复

使用道具 举报

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

本版积分规则

关注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