Display multiple custom cells in a UITableView?

后端 未结 2 1478
日久生厌
日久生厌 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:10

    If I understand your question correctly, the first infoCell (second UITableView row) should display the first person object's data, right?

    Then it seems you want:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *sessionCellID = @"sessionID";
        static NSString *infoCellID = @"infoID";
    
        if( indexPath.row == 0 ) {
            SessionCellClass *cell = nil;
            cell = (SessionCellClass *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
            if( !cell ) {
                //  do something to create a new instance of cell
                //  either alloc/initWithStyle or load via UINib
            }
            //  populate the cell with session model
            return cell;
        }
        else {
            InfoCellClass *cell = nil;
            cell = (InfoCellClass *)[tableView dequeueReusableCellWithIdentifier:infoCellID];
            if( !cell ) {
                //  do something to create a new instance of info cell
                //  either alloc/initWithStyle or load via UINib
                // ...
    
                //  get the model object:
                myObject *person = [[self people] objectAtIndex:indexPath.row - 1];
    
                //  populate the cell with that model object
                //  ...
                return cell;
            }
        }
    

    and you need to return [[self people] count] + 1 for the row count:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[self people] count] + 1;
    }
    

    so that the n'th row shows the (n-1)th data.

提交回复
热议问题