UITableView willDisplayCell表示奇怪,细胞边界仅在重用后调整

时间:2014-08-27 23:33:42

标签: objective-c uitableview ios7 border

我试图在单元格和屏幕边缘之间有10个像素的间隙。我实现了willDisplayCell:forRowAtIndexPath以及heightForCell,因为我在表视图中使用了两个不同的原型单元格。

我遇到的问题真的很奇怪。只有当我拉出列表时才会调整单元格,只有当它们被重用时才会猜测!请检查图像。![在此处输入图像说明] [1]

这是初始屏幕: https://www.dropbox.com/s/epl2n8a5zohhdmz/Edited.screenShot.01.png?dl=0

这是滚动表格视图后的屏幕: https://www.dropbox.com/s/c81dx9wmaq0mgr5/Edited.screenShot.02.png?dl=0

这是我的代码:

    - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell   forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.layer.borderWidth = 5.0f;
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    cell.layer.cornerRadius = 7.0;
    cell.layer.bounds = CGRectMake(0, 0, 260, cell.bounds.size.height - 10);
    cell.backgroundColor = [UIColor colorWithRed:207/255.0 green:207/255.0 blue:207/255.0 alpha:1.0];
    CGRect cellFrame = CGRectMake(20, cell.frame.origin.y, 280, cell.frame.size.height);
    cell.frame = cellFrame;
    cell.contentView.frame = cellFrame;
    [cell.contentView alignmentRectForFrame:cellFrame];
    [cell setNeedsLayout];
    }

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.namesList[indexPath.row] objectForKey:@"Relation"] length] == 0) {
        return 69.0;
    } else {
        return 89.0;
    }
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSMutableString *details = [[NSMutableString alloc] init];
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    UILabel *nameLabel;
    UILabel *relationLabel;
    UILabel *ageLabel;
    UILabel *dayLabel;
    UILabel *dateLabel;
    Helper *dateConvert = [[Helper alloc] init];

    if ([[self.namesList[indexPath.row] objectForKey:@"Relation"] length] == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell3" forIndexPath:indexPath];
        relationLabel = (UILabel *) [cell viewWithTag:2];
        relationLabel.text = [self.namesList[indexPath.row] objectForKey:@"Relation"];
    }
    nameLabel = (UILabel *) [cell viewWithTag:1];
    ageLabel = (UILabel *) [cell viewWithTag:3];
    dayLabel = (UILabel *) [cell viewWithTag:4];
    dateLabel = (UILabel *) [cell viewWithTag:5];

    [details appendString:[self.namesList[indexPath.row] objectForKey:@"Age"]];
    [details appendString:@")"];
    ageLabel.text = details;

    dayLabel.text = [dateConvert getWeekDay:[self.namesList[indexPath.row] objectForKey:@"Date"]];
    dateLabel.text = [self.namesList[indexPath.row] objectForKey:@"Date"];

    return cell;
}

我花了好几天试图找到问题:(非常感谢每一位帮助:)

1 个答案:

答案 0 :(得分:0)

为什么不让您的生活更轻松,并在Interface Builder中设计单元格以获得10 px空间。你可以添加一个UIView,从顶部,左侧,右侧和底部制作10像素,然后将UI元素放在其中。我建议将UITableViewCells子类化,并在其中包含代码以显示数据并设置边框。