Reputation: 251
Im creating a tableview. In the cellforrowatindexpath method i create a label and a imageview. Everything works well till here. But the cells are misplaced upon scrolling the tableview. this is the code i used:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NameLabel = [UILabel new];
NameLabel.frame = CGRectMake(143, 8, 95, 25);
NameLabel.font = [UIFont fontWithName:@"BBCNassim" size:16.0];
//NameLabel.highlightedTextColor = [UIColor whiteColor];
NameLabel.textAlignment = NSTextAlignmentRight;
NameLabel.textColor = [UIColor darkGrayColor];
[cell.contentView addSubview:NameLabel];
IconLabel = [UIImageView new];
IconLabel.frame = CGRectMake(245, 8, 24, 24);
[cell.contentView addSubview:IconLabel];
}
if (indexPath.section == 0)
{
[NameLabel setText:[self.menuItemsFirst objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsFirst objectAtIndex:indexPath.row]];
}
if (indexPath.section == 1)
{
[NameLabel setText:[self.menuItemsSecond objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsSecond objectAtIndex:indexPath.row]];
}
if (indexPath.section == 2)
{
[NameLabel setText:[self.menuItemsThird objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsThird objectAtIndex:indexPath.row]];
}
return cell;
}
Upvotes: 0
Views: 355
Reputation: 611
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NameLabel = [UILabel new];
NameLabel.frame = CGRectMake(143, 8, 95, 25);
NameLabel.font = [UIFont fontWithName:@"BBCNassim" size:16.0];
//NameLabel.highlightedTextColor = [UIColor whiteColor];
NameLabel.textAlignment = NSTextAlignmentRight;
NameLabel.textColor = [UIColor darkGrayColor];
[cell.contentView addSubview:NameLabel];
IconLabel = [UIImageView new];
IconLabel.frame = CGRectMake(245, 8, 24, 24);
[cell.contentView addSubview:IconLabel];
if (indexPath.section == 0)
{
[NameLabel setText:[self.menuItemsFirst objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsFirst objectAtIndex:indexPath.row]];
}
if (indexPath.section == 1)
{
[NameLabel setText:[self.menuItemsSecond objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsSecond objectAtIndex:indexPath.row]];
}
if (indexPath.section == 2)
{
[NameLabel setText:[self.menuItemsThird objectAtIndex:indexPath.row]];
[IconLabel setImage:[self.menuImgsThird objectAtIndex:indexPath.row]];
}
return cell;
}
Upvotes: 1
Reputation: 1818
Seems to me that your NameLabel
and IconLabel
are global variables. And all the different sets of data for each cell are overwriting the same elements.
You should create the labels locally in cellForRowAtIndexPath
and add them to each cell.
Upvotes: 1
Reputation: 12220
The proper way to request a reusable cell in table view is:
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
... so add forIndexPath:indexPath
Upvotes: 0