为什么我的动态原型细胞重叠?

时间:2018-03-08 04:18:31

标签: ios objective-c uitableview

当我使用动态原型时,我发现我的标签是重叠的。

“firstLabel”是我在故事板上插入的标签的默认文本,我在程序运行时设置了文本“ios”。

以下是我的代码。

@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;
}

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

感谢大家的回复。我发现当我使用两个cellID时,它们可能是混合的。在我只设置一个cellID之后,它就会正确。

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

更改为NSString* cellID=@"cell1";

答案 1 :(得分:-1)

因为firstLabel的高度等于单元格。和自定义标签上面的第一个标签。

enter image description here

相关问题