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

标签 ios uitableview reuseidentifier

我是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)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 action:@selector(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;   
}

最佳答案

您永远不会返回单元格,因为 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/

相关文章:

ios - 访问/获取已存在的 UIViewController 的引用(模拟后退按钮)

ios - 在 Swift 中运行应用程序时无法保存输入到 UITableView 的文本

ios - 从 iOS 中的 plist 文件获取值

iphone - UITableView 中的数据加载就像 iBooks 搜索表中一样

ios - 使用reuseIdentifier初始化UICollectionViewCell

ios - UITableView 中的重用问题

ios - 滚动时 UITableviewCell 显示另一个单元格的文本

ios - 为什么更改 Sprite 名称后 Cocos2d 上的应用会崩溃?

ios - UIBarButtonItem无法正确显示

ios - 从 Collection View 中选择时,MWPhotoBrowser 未显示正确的图像