簡體   English   中英

滾動后iOS UITableView Cell加載不正確?

[英]iOS UITableView Cell loads incorrectly after scroll?

我在視圖中有2個UITableView,我只在第2個表第5行和第7行添加了UITableViewCellAccessoryDisclosureIndicator。

但是在向下滾動第二個表(第1行消失)然后滾動回到頂部(第1行出現)之后,第1行現在有了UITableViewCellAccessoryDisclosureIndicator ?! 第1行以某種方式成為第5行或第7行??? 下面是我的cellForRowAtIndexPath代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.textColor = [UIColor blueColor];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    if (tableView == table1)
    {
        cell.textLabel.text = [title1 objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [list1 objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else if (tableView == table2)
    {
        cell.textLabel.text = [title2 objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [list2 objectAtIndex:indexPath.row];
        if (indexPath.row == 5 || indexPath.row == 7)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
    return cell;
}

非常感謝!

重用UITableViewCells以優化性能。 這發生在[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 在每次調用tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath您需要在單元格上顯式設置任何屬性tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

這樣的事情可以解決這個問題:

if (indexPath.row == 5 || indexPath.row == 7)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

暫無
暫無

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

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