当我向下滚动UITableView时,UITableViewCell获取默认图像

时间:2011-12-29 13:06:26

标签: iphone objective-c uitableview ios5

当我检查一个单元格时,相关的图像被更改,但是当我向下滚动时,单元格得到了它的默认图像,这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self->tableContents objectForKey:
                        [self->sortedKeys objectAtIndex:[indexPath section]]];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];

BOOL bChecked = NO;//By default, it's unchecked, so NO

/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? checked : unchecked); 
return cell;

}

修改

我已经编辑了我的代码,如上所示,但我仍然遇到问题,当我向上滚动表格视图时,默认图像会回来。

编辑2: @Openside:所以根据你的方法,我的代码片应该是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self->tableContents objectForKey:
                        [self->sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }
    NSUInteger row = [indexPath row];
    textClass *myText = (textClass*)[listData objectAtIndex:row];
    cell.textLabel.text = myText.text;
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
    UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"checked.png"]];



    cell.accessoryView = (myText.isChecked ? checked : unchecked); 
    return cell;
}


- (void)tableView:(UITableView *)tableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    textClass *myText=[[textClass alloc]init];
    myText.isChecked=YES;
}

我遇到了这个例外:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString text]: unrecognized selector sent to instance 0x673bc'

指示异常问题的行是(在cellForRowAtIndexPath委托方法中):

cell.textLabel.text = myText.text;

我添加了类textClass虽然我不太相信,但我想我可以在我的UITableView的holder类中使用一个属性。

3 个答案:

答案 0 :(得分:2)

正如Openside所说,在滚动时,细胞将来自出列。因此,我们需要根据新状态分配图像。

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = ...; ///< reuse a created cell.
    if(cell == nil) {
        cell = ...; ///< create a new cell
    }

    BOOL bChecked = ...///< Decide the cell checked or not.

    /// Assign cell according to `bChecked`
    cell.accessoryView = (bChecked ? imgChecked : imgNonChecked); 
    return cell;

}

Edit:
至于我,我将通过tableView的方法indexPathForSelectedRow来决定检查状态。我假设你的表视图不是多重选择。它一次只能选择一行。

因此,我通过以下方式决定检查状态。因为tableView包含有关选择哪个indexPath的信息。

NSIndexPath *selIndexPath = [tableView indexPathForSelectedRow];
BOOL bChecked = [indexPath isEqual:selIndexPath];

答案 1 :(得分:1)

问题是当您将其滚出视图然后返回视图时,将刷新单元格(或重新使用dequeueReusableCellWithIdentifier)。您需要在源对象上有一个标志,说明它是否已被选中,然后添加if语句以确定是否使用了checked.png或unchecked.png图像。

但是,查看代码源代码对象只是文本,可能值得创建一个带有两个属性的子类NSObject

文字和 检查

然后您的cellForRowAtIndexPath代码可以确定要显示的图像。

我希望有所帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView

     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"checked.png"]];



/// Assign cell according to `bChecked`
cell.accessoryView = (myText.isChecked ? checked : unchecked); 
return cell;

}

@interface textClass : NSObject {

@private
    NSString *_text;
    BOOL _isChecked;
}

@property (nonatomic, retain) NSString *text;
@property (nonatomic, assign) BOOL isChecked;

@end

@implementation textClass

@synthesize text = _text;
@synthesize isChecked = _isChecked;

@end

好的,我在这里创建了一个名为textClass的类,它有两个属性文本和isChecked。加载视图时,使用您之前使用的文本使用这些对象填充NSMutableArray(listData)。检查单元格时,在重复使用单元格时将isChecked属性设置为YES,此属性在委托方法之外保留其状态,并且应正确呈现。

答案 2 :(得分:0)

将完整的代码放在括号中:

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self->tableContents objectForKey:
                        [self->sortedKeys objectAtIndex:[indexPath section]]];

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

    NSUInteger row = [indexPath row];
    textClass *myText = (textClass*)[listData objectAtIndex:row];
    cell.textLabel.text = myText.text;
    UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
    UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"checked.png"]];



    cell.accessoryView = (myText.isChecked ? checked : unchecked); 
}
    return cell;
}  

这绝对有用。

相关问题