簡體   English   中英

UITableView滾動和重繪問題

[英]UITableView scrolling and redraw issue

我知道,如果我在自定義單元格中添加了一些圖像和子視圖,則必須重用該單元格,以便自定義控件不會出現在其他單元格上,但是這里還有其他問題。 我只想在第一部分的第一個單元格上有ImageView,所以在以下代碼中使用了IndexPath.Section == 0和IndexPath.Row == 0條件,但是問題是當我滾動表時,另一個單元格會滿足此條件,我的代碼也會在該單元格上創建imageview。 我已經嘗試過標記它並使用相同的標記的cellView,但它也無濟於事。 單元問題是禁用少數單元的用戶交互。 最終,在滾動之后,它將禁用所有單元的用戶交互。 反正有解決辦法嗎?

謝謝。

- (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] autorelease];
}

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
    [imageView setFrame: CGRectMake(10, 10, 54, 54)];
    [cellView addSubview:imageView];
    cell.backgroundView = cellView;

    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }
    cell.textLabel.text = title;
    cell.userInteractionEnabled = NO;
    return cell;
}

cell.textLabel.text = @"Test";
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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    cell.imageView.image = imageView.image;
    cell.textLabel.text = nil;
    cell.textLabel.textColor = [UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = YES;
    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }

    cell.imageView.image = nil;
    cell.textLabel.text = title;
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = NO;

    return cell;
}


cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}

[已驗證的代碼]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *NormalCellIdentifier = @"NormalCell";
static NSString *TitleCellIdentifier = @"TitleCell";
NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) {
    neededCellType = TitleCellIdentifier;
} else {
    neededCellType = NormalCellIdentifier;
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

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

    //Only add content to cell if it is new
    if([neededCellType isEqualToString: TitleCellIdentifier]) {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
        cell.imageView.image = imageView.image;
    }
}

if([neededCellType isEqualToString: NormalCellIdentifier]) {
    NSString * title;
    if(indexPath.row == 0) {
        switch (indexPath.section) {
            case 1:
                title = @"Friends";
                break;
            case 2:
                title = @"Accounts";
                break;
            case 3:
                title = @"Stats";
                break;
            default:
                title = nil;
                break;
        }
        cell.textLabel.text = title;
        cell.textLabel.textColor = [UIColor redColor];
        cell.userInteractionEnabled = NO;
    } else {

        cell.userInteractionEnabled = YES;
        cell.textLabel.textColor = [UIColor blueColor];
        cell.textLabel.text = @"Test";
    }
}

return cell; 
}

我認為您的問題是單元的重用使得它不能被作為新單元創建的單元具有一些必須重新定義的屬性。 例如,嘗試將cell.userInteractionEnabled = YES分配給所有其他情況,然后查看結果。

問題在於,您不允許以后正確重用了正確顯示圖像的單元格,並且圖像視圖仍在其中。

這是兩個解決方案:

  1. 在創建圖像視圖時設置它的標記值,然后在設置單元格時包括代碼以檢查並刪除舊的imageView(如有必要)。

  2. 為需要圖像視圖的單元格和不需要圖像視圖的單元格分配不同的重用標識符。 然后,請確保僅在創建單元時才向它們添加新的圖像視圖,而不是在重用它們時向它們添加新的圖像視圖。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *NormalCellIdentifier = @"NormalCell"; static NSString *TitleCellIdentifier = @"TitleCell"; NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) { neededCellType = TitleCellIdentifier; } else { neededCellType = NormalCellIdentifier; }

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

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

//Only add content to cell if it is new if([neededCellType isEqualToString: TitleCellIdentifier]) { UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake(10, 10, 54, 54)]; [cellView addSubview:imageView]; cell.backgroundView = cellView; } }

if([neededCellType isEqualToString: NormalCellIdentifier]) { NSString * title; if(indexPath.row == 0) {

switch (indexPath.section) {
    case 1:
        title = @"Friends";
        break;
    case 2:
        title = @"Accounts";
        break;
    case 3:
        title = @"Stats";
        break;
    default:
        title = nil;
        break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;

}

else { cell.textLabel.text = @"Test"; return cell; } } }

(最后幾行從代碼框中掉了出來)。 那應該做。

暫無
暫無

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

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