Display multiple custom cells in a UITableView?

后端 未结 2 1496
日久生厌
日久生厌 2021-01-03 12:46

I am using Xcode 4.2 on SnowLeopard, and my project is using storyboards. I am trying to implement a UITableView with 2 different custom cell types, sessi

2条回答
  •  情歌与酒
    2021-01-03 13:05

    if you look at the if else section it shows that the first row is an "sessionCell" and all other rows are "infoCells" what i think you want to do is make the first row an sessionCell, the second row an infoCell and all of the other rows peopleCells

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.people count] + 2; // Added two for sessionCell & infoCell
    }
    
    //inside cellForRowAtIndexPath
    if(indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
    } else if (indexPath.row == 1 {
        cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"];
        Person *person = [self.people objectAtIndex:index.path.row - 2];
    }
    
    ...
    return cell;
    

    Better yet, I would try to make two different sell sections, one for info and one for people

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return section == 0 ? 2 : self.people.count
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell = nil;
        if (indexPath.section == 0) {
            if(indexPath.row == 0) {
                cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
                // configure session cell
            } else if (indexPath.row == 1 {
                cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
                // configure info cell
            }
        } else {
             cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
             Person *person = [self.people objectAtIndexPath:indexPath.row];
             // configure person cell
        }
    
        return cell;
    }
    

提交回复
热议问题