簡體   English   中英

UITableView重繪滾動問題

[英]UITableView redraw issue on scrolling

我有一個從列表填充的分組UITableView
在某些行上,我不想公開,在某些行上,我需要添加標簽。
但是是否以某種方式混合並在錯誤的行上添加標簽並在每行顯示披露。
我在這里做錯了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell == nil)
     {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;       
            cell.accessoryView = [[ UIImageView alloc ]
                                  initWithImage:[UIImage imageNamed:@"customdisclosure.png" ]];
     }

     NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
     NSArray *array = [dictionary objectForKey:@"Items"];
     NSString *cellValue = [array objectAtIndex:indexPath.row];
     cell.textLabel.text = cellValue;

     if([cell.textLabel.text isEqualToString:@"with label"])
     {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.detailTextLabel.textColor = [UIColor blackColor];
            cell.detailTextLabel.text = @"label...";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
     }
     else if([cell.textLabel.text isEqualToString: @"No disclosure" ])
     {
            cell.accessoryType = UITableViewCellAccessoryNone;
     }

     return cell;
}

else if子句中,您不會清除重用單元格上的cell.detailTextLabel的文本。 將其設置為nil,就可以了。

cell.detailTextLabel.text = nil;

您還需要在else if子句中隱藏accessoryView視圖,然后取消隱藏它。

cell.accessoryView.hidden = YES;

總的來說,我會考慮對UITableViewCell子類化,以便您可以重寫prepareForReuse來為下一個cellForRowAtIndexPath調用重置單元格。

猜猜問題出在使用可重用的標識符。 對不需要的附件視圖使用不同的單元格標識符。

static NSString *CellWithDisclosure = @"CellID_WithDisclosure";
static NSString *CellWithNoDisclosure = @"CellID_NoDisclosure";

NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];

if([cell.textLabel.text isEqualToString:@"with label"])
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithDisclosure];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if([cell.textLabel.text isEqualToString: @"No disclosure" ])
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithNoDisclosure];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
}

cell.textLabel.text = cellValue;
return cell;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM