简体   繁体   中英

why my dynamic prototypes cell overlapping?

When I use dynamic prototypes, I found my label is overlapping.

"firstLabel" is the default text of the label I have inserted on storyboard and I set the text "ios" when the program run.

Following are my code.

@implementation ViewController{
    NSArray* _books;
}
- (void)viewDidLoad {
    _books=@[@"ios",@"android"];
    self.table.dataSource=self;
    self.table.delegate=self;
    // Do any additional setup after loading the view, typically from a nib.
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row=indexPath.row;
    NSString* cellID=(row%2==0?@"cell1":@"cell2");
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    UILabel* label=(UILabel*)[cell viewWithTag:1];
    label.text=_books[row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _books.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

在此处输入图片说明在此处输入图片说明

Thank everyone for your replies. I found when I use two cellIDs, they maybe mixed. After I set only one cellID, it gets right.

NSString* cellID=(row%2==0?@"cell1":@"cell2");

change to NSString* cellID=@"cell1";

because the firstLabel's height is equal to the cell. and the firstLabel in the above of the custom label.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM