2

私は iOS 開発の新入生です。1 つの uitableview のさまざまなセクションにさまざまなセル スタイルを実装したいと考えています。私のテーブルビューには2つのセクションがあり、各セクションには3つの行があります。

再利用セルを取得するには、セルごとに異なる reuseidentifier が必要であることはわかっていますが、アプリをコンパイルすると、コンソールには常に次の情報が表示されます。

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;   
}
4

2 に答える 2

2

セルを構成するためのより良い方法

 if (indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            //configure you cell here..
            break;

        case 1:
            //configure you cell here..
            break;

        case 2:
            //configure you cell here..
            break;

        default:
            break;
    }
} else if (indexPath.section == 1) {
    switch (indexPath.row) {
        case 0:
            //configure you cell here..
            break;

        case 1:
            //configure you cell here..   
             break;

        case 2:
            //configure you cell here..                
            break;

        default:
            break;
    }
} else if (indexPath.section == 2) {
    switch (indexPath.row) {
        case 0:
        //configure you cell here..                
        break;

        case 1:
        //configure you cell here..                
        break;

        default:
            break;
    }
} 
}
return cell;
于 2012-08-26T07:01:09.307 に答える
2

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 のいずれかになるため、省略してもかまいません。

于 2012-08-26T06:08:46.683 に答える