UITableView Mix of Static and Dynamic Cells?

前端 未结 6 1393
無奈伤痛
無奈伤痛 2020-11-28 07:57

I know you cannot mix Static and Dynamic cell types in a single UITableView but I couldn\'t think of a better way to describe my issue.

I have several p

6条回答
  •  隐瞒了意图╮
    2020-11-28 08:29

    As you stated you can't mix static and dynamic cells. However, what you can do is break up the content into different data arrays that correspond to each group. Then break the table up into difference sections and load the data from the correct array in cellForRowAtIndexPath:.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"CELLID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
        switch (indexPath.section) {
            case 0:{
                cell.textLabel.text = self.arrayOfStaticThings1[indexPath.row];
            }break;
    
            case 1:{
                cell.textLabel.text = self.arrayOfDynamicThings[indexPath.row];
            }break;
    
            case 2:{
                cell.textLabel.text = self.arrayOfStaticThings2[indexPath.row];
            }break;
    
            default:
                break;
        }
    
        return cell;
    }
    
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        switch (section) {
            case 0:{
                return self.arrayOfStaticThings1.count;
            }break;
    
            case 1:{
                return self.arrayOfDynamicThings.count;
            }break;
    
            case 2:{
                return self.arrayOfStaticThings2.count;
            }break;
    
            default:
                return 0;
                break;
        }
    }
    

提交回复
热议问题