在表上使用多个UITableViewCell

时间:2014-10-07 04:29:54

标签: ios uitableview

我正在尝试在桌面上使用两种不同类型的自定义单元格。我的想法是以下

2 - 部分 1 - 行

-- Section 1
 - Row

-- Section 2
 - Row

多数民众赞成!我收到了这个错误:

  

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'UITableView dataSource   必须从tableView:cellForRowAtIndexPath:'

返回一个单元格
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 10.; // you can have your own choice, of course
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString *CellIdentifier = @"Cell";
            HeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[HeaderCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                         reuseIdentifier:CellIdentifier];
            }
            PFFile *eventImage = [self.event objectForKey:@"eventPoster"];
            cell.headerImage.file = eventImage;
            [cell.headerImage loadInBackground];
            NSAttributedString *eventNameAtt;
            eventNameAtt = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [self.event[@"eventName"]uppercaseString]]
                                                           attributes:@{NSFontAttributeName: [UIFont fontWithName:@"ProximaNova-Semibold" size:15],
                                                                        NSStrokeColorAttributeName : [UIColor whiteColor],
                                                                        NSKernAttributeName : @(3.0F)}];
            cell.eventLabel.attributedText = eventNameAtt;
            cell.eventLabel.textColor = [UIColor whiteColor];
            NSAttributedString *eventLocation;
            eventLocation = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [self.eventLocation[@"Name"]uppercaseString]]
                                                            attributes:@{NSFontAttributeName: [UIFont fontWithName:@"ProximaNova-Light" size:11],

                                                                         NSStrokeColorAttributeName : [UIColor whiteColor],

                                                                         NSKernAttributeName : @(3.0F)}];

            cell.eventLabel.attributedText = eventNameAtt;
            cell.eventLabel.textColor = [UIColor whiteColor];
            cell.placeLabel.attributedText = eventLocation;
            cell.placeLabel.textColor = [UIColor whiteColor];
            return cell;
    }
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                static NSString *CellIdentifier = @"About";
                AboutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[AboutCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                            reuseIdentifier:CellIdentifier];
                    cell.testLabel.text = @"Test";
                    cell.testLabel.textColor = [UIColor blackColor];
                    return cell;
                }
            }
        }
    }

}
            return nil;

    }

1 个答案:

答案 0 :(得分:1)

首先,我要移除内部IF块,因为如果每个部分只有一行,那么该行始终为零。

其次,在第1个区块内,return cell;位于内部IF块内,因此如果单元格成功出列,则不会调用它。将其移到if (cell == nil)区块之外。

因此:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"Cell";
        HeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[HeaderCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                     reuseIdentifier:CellIdentifier];
        }
        PFFile *eventImage = [self.event objectForKey:@"eventPoster"];
        cell.headerImage.file = eventImage;
        [cell.headerImage loadInBackground];
        NSAttributedString *eventNameAtt;
        eventNameAtt = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [self.event[@"eventName"]uppercaseString]]
                                                       attributes:@{NSFontAttributeName: [UIFont fontWithName:@"ProximaNova-Semibold" size:15],
                                                                    NSStrokeColorAttributeName : [UIColor whiteColor],
                                                                    NSKernAttributeName : @(3.0F)}];
        cell.eventLabel.attributedText = eventNameAtt;
        cell.eventLabel.textColor = [UIColor whiteColor];
        NSAttributedString *eventLocation;
        eventLocation = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [self.eventLocation[@"Name"]uppercaseString]]
                                                        attributes:@{NSFontAttributeName: [UIFont fontWithName:@"ProximaNova-Light" size:11],

                                                                     NSStrokeColorAttributeName : [UIColor whiteColor],

                                                                     NSKernAttributeName : @(3.0F)}];

        cell.eventLabel.attributedText = eventNameAtt;
        cell.eventLabel.textColor = [UIColor whiteColor];
        cell.placeLabel.attributedText = eventLocation;
        cell.placeLabel.textColor = [UIColor whiteColor];
        return cell;
    }
    if (indexPath.section == 1) {

        static NSString *CellIdentifier = @"About";
        AboutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[AboutCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                    reuseIdentifier:CellIdentifier];

        }

        cell.testLabel.text = @"Test";
        cell.testLabel.textColor = [UIColor blackColor];
        return cell;
    }
return nil;
}