Maysam
Maysam

Reputation: 7367

addSubView does not work properly on UITableView

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

Upvotes: 1

Views: 3563

Answers (3)

Baby Groot
Baby Groot

Reputation: 4279

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

Upvotes: 1

Sulthan
Sulthan

Reputation: 130102

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

Upvotes: 2

iPatel
iPatel

Reputation: 47059

- (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;

}

Upvotes: 0

Related Questions