[英]Tableview cells misplaced upon scrolling
我正在創建一個tableview。 在cellforrowatindexpath方法中,我創建了一個標簽和一個imageview。 到這里一切都很好。 但是在滾動表格視圖時,單元格的位置不正確。 這是我使用的代碼:
- (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;
}
嘗試這個,
- (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;
}
在我看來,您的NameLabel
和IconLabel
是全局變量。 每個單元的所有不同數據集都覆蓋相同的元素。
您應該在cellForRowAtIndexPath
本地創建標簽,並將其添加到每個單元格中。
在表視圖中請求可重用單元格的正確方法是:
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
...因此添加forIndexPath:indexPath
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.