1

This code should draw a line over cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil==cell){
        cell = [[UITableViewCell alloc]
                                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    NSString *currentCourseName=[courseKeys objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:currentCourseName];
    NSLog(currentCourseName);
    CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font];
    CGFloat y = cell.contentView.frame.size.height / 2;

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5,y,size.width, 3)];
    line.backgroundColor = [UIColor redColor];
    [cell.textLabel addSubview:line];

    return cell;

}

But it seems I have to scroll it several times to see red lines. enter image description here

4

3 に答える 3

2

I see two problems in your code.

First

CGFloat y = cell.contentView.frame.size.height / 2;

What is the frame of the contentView when you create the cell? If you are adding the subview to the label, use label's height.

CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5, cell.textLabel.bounds.size.height / 2.0f - 1.5f, size.width, 3)];
line.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

Second

[cell.textLabel addSubview:line];

If a cell is reused, you are adding lines to cells when there is already a line. Check for textLabel subviews count first.

if (cell.textLabel.subviews.count == 0) {
    [cell.textLabel addSubview:line];
}
于 2013-03-25T12:38:40.637 に答える
1

I think the view that your are adding in that, rather than adding that in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath mehod, try using custom cell concept. That solved this issue in my UITableView.

Creating a custom Table View Cell programmatically

Custom Table View Cells in iPad Programming

于 2013-03-25T12:47:13.140 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil==cell){
        cell = [[UITableViewCell alloc]
                                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

     NSString *currentCourseName=[courseKeys objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:currentCourseName];
    NSLog(currentCourseName);
    CGSize size = [cell.textLabel.text sizeWithFont:cell.textLabel.font];
    CGFloat y = cell.contentView.frame.size.height / 2;

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5,y,size.width, 3)];
    line.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:line];

    }



    return cell;

}
于 2013-03-25T12:29:24.373 に答える