Ahmed Z. Ahmed Z. - 6 months ago 30
iOS Question

UITableViewCell Repeating UIView

I am facing a small issue and em banging my head for being not able to solve it…

I have a

UITableView
with Custom
UITableViewCell
.. the cell's height is determined on the size of text. when the user taps it, the text is translated and the translated text is appended to it in new line. for making them look separated, I add a
UIView lineView
with width as much as cell's label and
height = 1


UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(bubbleFrame.origin.x, bubbleFrame.origin.y + bubbleFrame.size.height/2, bubbleFrame.size.width, 1)];
lineView.backgroundColor = [UIColor whiteColor];
lineView.tag = indexPath.row;
[cell addSubview:lineView];


Everything works fine but when i navigate back to that
viewController
i see that
lineView
misplaced on each cell..

Cant understand why its doing that..

Any help would be appreciated.. Thanks in advance.. Image

Answer

Well after some working on the advice of Mustafa Ibrahim, i found another question that had similar problem … so thanks to intropedro's answer problem solved :)

added this with dequeueReusableCellWithIdentifier

        if (cell == nil) {
            cell = [[ChatCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        else{
            [[cell.contentView viewWithTag:500] removeFromSuperview];
        }

and changed my lineView code a bit too..

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(bubbleFrame.origin.x, bubbleFrame.origin.y + bubbleFrame.size.height/2, bubbleFrame.size.width, 1)];
                    lineView.backgroundColor = [UIColor whiteColor];
                    lineView.tag = indexPath.row;
                    [cell addSubview:lineView];
Comments