Multiple Custom Rows UITableView?

后端 未结 2 1685
鱼传尺愫
鱼传尺愫 2021-01-01 06:55

I\'ve been searching a lot but didn\'t find anything useful related to multiple custom rows, I need to create a settings tableView for my app,in which I need to load the row

2条回答
  •  清酒与你
    2021-01-01 07:27

    If you're not already familiar with loading a custom cell from a xib, check out the documentation here. To extend this to multiple custom xibs, you can create each table cell in a separate xib, give it a unique cell identifier, set the table view controller as the file's owner, and connect each cell to the custom cell outlet you define (in the docs, they use tvCell as this outlet). Then in your -tableView:cellForRowAtIndexPath: method, you can load the correct xib (or dequeue the correct reuseable cell) by checking which row you're providing a cell for. For example:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *MyIdentifier1 = @"MyIdentifier1";
        static NSString *MyIdentifier2 = @"MyIdentifier2";
        static NSString *MyIdentifier3 = @"MyIdentifier3";
        static NSString *MyIdentifier4 = @"MyIdentifier4";
    
        NSUInteger row = indexPath.row
    
        UITableViewCell *cell = nil;
    
        if (row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer1];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell1" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 1) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer2];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell2" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 2) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer3];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell3" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 4) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer4];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell4" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        // etc.
    
        // Do any other custom set up for your cell
    
        return cell;
    
    }
    

提交回复
热议问题