UITableView的viewForHeaderInSection方法:访问单元格的可视属性

时间:2014-03-05 04:53:36

标签: ios objective-c

我使用UITableView委托方法为我的viewForHeaderInSection定义标题行,我想从第一行获取X coodinate来定位一些标签,因为我正在移动行中的标签不断,并希望在故事板中定义这些坐标。但它不起作用:X坐标始终为0.这是尝试:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    float x = tableView.frame.origin.x;
    float y = tableView.frame.origin.y;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y - 22, tableView.bounds.size.width, 12)];
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    view.userInteractionEnabled = YES;
    view.backgroundColor = [UIColor lightGrayColor];

    LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];

    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_DATE.frame.origin.x, 0, 65, 22)];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.font = [UIFont systemFontOfSize:12.0];
    dateLabel.text = @"DATE";

    UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.label_LOC.frame.origin.x, 0, 65, 22)];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.font = [UIFont systemFontOfSize:12.0];
    locationLabel.text = @"LOC";

    [view addSubview:dateLabel];
    [view addSubview:locationLabel];

    return view;
}

是否有某种方法可以从UITableView正确获取行以确定单元格标签的X / Y坐标?

1 个答案:

答案 0 :(得分:0)

这一行:

LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:0];

与:

相同
LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:nil];

您没有传入索引路径:

试试这个:

LogCell *cell = (LogCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];