Multiple custom UITableView strings?

I searched a lot, but did not find anything useful related to several custom strings, I need to create a settings table for my application in which I need to load strings from xib files, for example:

ROW 1 = → XIB 1.
ROW 2 = → XIB 2.
ROW 3 = → XIB 3.
ROW 4 = → XIB 4.

My real code is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=nil;
    //We use CellType1 xib for certain rows
    if(indexPath.row==0){
        static NSString *CellIdentifier = @"ACell";
        cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil];
            cell = (ACell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelA setText:@"myText"]
    }
    //We use CellType2 xib for other rows
    else{
        static NSString *CellIdentifier = @"BCell";
        cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil];
            cell = (BCell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelB setText:@"myText"]
    }

    return cell;
}
+2
source share
2 answers

First you create some custom UITableViewCell classes (.h and .m), as many xib files as you have:
That way you could use CellType1 and CellType2. CellType1.h would look like

#import <UIKit/UIKit.h>
@interface CellType1 : UITableViewCell

@property(nonatomic,strong) IBOutlet UILabel *customLabel;

@end

xib , , , UITableViewCell CellType1. CellType2.

TableViewController cellForRow :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
//We use CellType1 xib for certain rows
if(indexPath.row==<whatever you want>){
     static NSString *CellIdentifier = @"CellType1";
     cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell==nil){
        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
        cell = (CellType1 *)[nib objectAtIndex:0];
      }
      //Custom cell with whatever
      [cell.customLabel setText:@"myText"]
}
//We use CellType2 xib for other rows
else{
    static NSString *CellIdentifier = @"CellType2";
    cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell==nil){
        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
        cell = (CellType2 *)[nib objectAtIndex:0];
      }
      //Custom cell with whatever
      [cell.customLabel setText:@"myText"]
}

return cell;
}
+7

xib, . xib, xib, , , ( docs, tvCell ). -tableView:cellForRowAtIndexPath: xib ( ), , . :

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

}
+3

All Articles