Reputation: 7310
I thought my table was fine until I changed the backgrounds to be a little transparent, which showed previous cells underneath new dequeued cells. This is the code in my tableView:cellForRowAtIndexPath:
method. I took out redundant label set up, which are all the same as timeLabel
.
static NSString* cellIdentifer = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifer];
if (!cell)
{
cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];
}
int offset = 5;
int rowHeight = 120;
UIView* cellView = [[UIView alloc] initWithFrame: CGRectMake(offset, offset, 320 - offset * 2, rowHeight - offset * 2)];
cellView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithWhite: 1.0 alpha: .8 ] : [UIColor colorWithWhite: .3 alpha: .8];
int padding = 5;
int x1 = 5;
int x2 = CGRectGetWidth(cellView.frame) / 3;
int x3 = x2 * 2;
int y1 = 5;
int y2 = CGRectGetHeight(cellView.frame) / 2 + padding;
int width = CGRectGetWidth(cellView.frame) / 3 - padding;
int sHeight = CGRectGetHeight(cellView.frame) / 2 - padding;
int bHeight = CGRectGetHeight(cellView.frame) - padding;
CGRect cell1 = CGRectMake(x1, y1, width, sHeight);
int row = indexPath.row;// - 1;
UILabel* timeLabel = [[UILabel alloc] initWithFrame: cell1];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.text = [[self.tableData objectAtIndex: row] objectForKey: @"time"];
[cellView addSubview: timeLabel];
[cell.contentView addSubview: cellView];
Upvotes: 4
Views: 3706
Reputation: 1305
The main problem is with cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];
method so if you do not want to change anything then use method like this UITableViewCell *cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: nil];
This is not better way but you can use this. Because in this case your cell will not be reused. One better solution is that make custom cell with your choice and use it.
Upvotes: 0
Reputation: 39988
You are creating new UIView
and UILabel
even if the cell is reused which results showing old one below your new one.
Do like this
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifer];
if (!cell)
{
cell= [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifer];
int offset = 5;
int rowHeight = 120;
UIView* cellView = [[UIView alloc] initWithFrame: CGRectMake(offset, offset, 320 - offset * 2, rowHeight - offset * 2)];
cellView.tag = 200;
cellView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithWhite: 1.0 alpha: .8 ] : [UIColor colorWithWhite: .3 alpha: .8];
int padding = 5;
int x1 = 5;
int x2 = CGRectGetWidth(cellView.frame) / 3;
int x3 = x2 * 2;
int y1 = 5;
int y2 = CGRectGetHeight(cellView.frame) / 2 + padding;
int width = CGRectGetWidth(cellView.frame) / 3 - padding;
int sHeight = CGRectGetHeight(cellView.frame) / 2 - padding;
int bHeight = CGRectGetHeight(cellView.frame) - padding;
CGRect cell1 = CGRectMake(x1, y1, width, sHeight);
int row = indexPath.row;// - 1;
UILabel* timeLabel = [[UILabel alloc] initWithFrame: cell1];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.tag = 300;
[cellView addSubview: timeLabel];
[cell.contentView addSubview: cellView];
}
UIView *view = [cell.contentView viewWithTag:200];
UILabel *timeLabel = (UILabel*)[view viewWithTag:300];
timeLabel.text = [[self.tableData objectAtIndex: row] objectForKey: @"time"];
Instead of having this messy thing I would suggest you to create a custom UITableViewCell
Upvotes: 5