outime
outime

Reputation: 135

Using multiple UITableViewCell on a Table

I'm trying to use two different type of custom cell on my table. My idea its the following

2 - Sections 1 - Row

-- Section 1
 - Row

-- Section 2
 - Row

Thats, its! I'm getting this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from 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;

    }

Upvotes: 0

Views: 62

Answers (1)

siburb
siburb

Reputation: 5017

Firstly, I'd remove the inner IF blocks because if you've only got one row per section, then the row should always be zero.

Secondly, inside your section 1 block, your return cell; is inside the inner IF block, therefore it won't be called if a cell is successfully dequeued. Move it outside your if (cell == nil) block.

Therefore:

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

Upvotes: 1

Related Questions