-3

这就是我的表格视图的外观。 表视图

这是我的代码 当我向下滚动 有错误的图像 ViewController.h时会发生这种情况

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

{
    NSMutableArray *items ;

}
@property (nonatomic,retain) NSMutableArray *items ;
@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize items;
- (void)viewDidLoad
{
    [super viewDidLoad];

    items = [[NSMutableArray alloc] init];
    [items addObject:@"bla bla bla"];
    [items addObject:@"rikifhdif bla bla"];
    [items addObject:@"bla sdfkndskfnlk bla"];
    [items addObject:@"sdf,nsdmf bla bla"];
    [items addObject:@"sdjkfhksd bla bla"];
    [items addObject:@"dkfhkusd bla bla"];
    [items addObject:@"bla sdfjknskdjf bla"];
    [items addObject:@"kiidrfjifig jjojdjfgdfgjjdjsjkhksidfl"];
    [items addObject:@"mksjdhf ysdgjg bla"];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma tableview logic
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"Title0";
    }
    else if(section == 1)
    {
        return @"Title1";
    }
    else if(section == 2)
    {
        return @"Title2";
    }
    if(section == 3)
    {
        return @"Title3";
    }
    else if(section == 4)
    {
        return @"Title4";
    }
    else 
    {
        return @"Title5";
    }
    }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 3;
    }
    if (section==1) {
        return 1;
    }
    if (section==2) {
        return 2;
    }
    if (section==3) {
        return 2;
    }
    if (section==4) {
        return 1;
    }
    if (section==5) {
        return 1;
    }

    return 1;
}
-(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] autorelease];
    }
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
    switch (indexPath.section) {
        case 0:

            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];
            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }

            break;
        case 1:
            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];

            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 2:
            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];

            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 3:
            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];
            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 4:
            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];
            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 5:
            if (indexPath.row==1) {
                [[cell imageView] setHidden:YES];
            }
            if (indexPath.row==2) {
                [[cell imageView] setHidden:YES];
            }
            break;

        default:
            break;
    }

    return cell;
    //}
}

- (void)dealloc {
    [items release], items = nil;
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

只想显示每个部分的第一行的图像。加载时输出正常,但是当我滚动视图时,第一个图像消失了。我该如何解决这个问题?任何帮助,将不胜感激。提前谢谢了。

4

4 回答 4

3

当您向下滚动时,您的图像会消失,因为UITableView重复使用其单元格。比如说,对于第一部分的第 1 行,您要求提供单元格,并将其图像隐藏

case 0:
    if (indexPath.row==1) {
        [[cell imageView] setHidden:YES];
    }

现在,当您向下滚动时,足够聪明地知道不再使用UITableView该单元格(因为它不再在屏幕上可见),并且它将该单元格重用于出现在视图下方的新行。既然你已经隐藏了图像,它就被隐藏了。这里的关键是没有创建新的单元格,只有旧的单元格被重用,并且它们的状态没有改变。

要解决此问题,只需添加案例row == 0

case 0:
    if (indexPath.row==0) {
        [[cell imageView] setHidden:NO];
    }
    else if (indexPath.row == 1) {
        [[cell imageView] setHidden:YES];
    }
    else ...

我建议使用更清晰的语法:

case 0:
    cell.imageView.hidden = indexPath.row != 0;

或者,如果您只想要所有部分的所有第一行的图像:

cell.imageView.hidden = indexPath.row != 0 // No need of case expression
于 2013-06-22T10:54:21.477 回答
2

您已经为 Index-Path.row 设置了图像隐藏,例如:-

例如,您设置的第一个 部分if (indexPath.row==1) { [[cell imageView] setHidden:YES];}if (indexPath.row==2)这就是为什么您的第一个部分仅显示第 0 个索引行图像出现其他不显示的原因相同。

case 0:

    if (indexPath.row==0) {
        [[cell imageView] setHidden:NO];
    }
    else{
        [[cell imageView] setHidden:YES];
    }

只需cellForRowAtIndexPath用我的波纹管cellForRowAtIndexPath方法替换您的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];   //uniq identifire for each section with it's row

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

    switch (indexPath.section) {
        case 0:
           cell.textLabel.text = [items objectAtIndex:indexPath.row];
           cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];

            if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }

            break;
        case 1:
             cell.textLabel.text = [items objectAtIndex:indexPath.row];
             cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];

            if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 2:

              cell.textLabel.text = [items objectAtIndex:indexPath.row];
              cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];

           if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 3:

             cell.textLabel.text = [items objectAtIndex:indexPath.row];
              cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];

           if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 4:

             cell.textLabel.text = [items objectAtIndex:indexPath.row];
             cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
            if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }
            break;
        case 5:

             cell.textLabel.text = [items objectAtIndex:indexPath.row];
             cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];

            if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];
            }
            else {
                [[cell imageView] setHidden:YES];
            }
            break;

        default:
            break;
    }

    return cell;
    //}
}

像这样创建 CellIdentifier:-

 NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];   //uniq identifire for each section with it's row
于 2013-06-22T10:27:31.517 回答
0

表格视图单元格在出现时加载单元格,只需添加此代码所有案例 0:10 案例 5:

if (indexPath.row==0) {
                [[cell imageView] setHidden:NO];  
            }

我的建议你在所有情况下都减少你的代码:

if (indexPath.row==0) {
            [[cell imageView] setHidden:NO];
        }
        else {
            [[cell imageView] setHidden:YES];
        }
于 2013-06-22T10:33:38.887 回答
-1

我已经通过进行以下更改来解决它。但我不确定这样做是否正确。

//if(cell == nil) 
//{


    //}

我所做的是注释掉检查单元格是否为零的单元格条件。

于 2013-06-22T11:07:02.493 回答