簡體   English   中英

UITableViewCell在我上下滾動UITableView時獲取默認圖像

[英]UITableViewCell getting the default image when i scroll down and up the UITableView

當我檢查一個單元格時,關聯的圖像已更改,但是當我上下滾動時,該單元格獲得了其默認圖像,這是我的代碼:

- (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類中使用屬​​性。

正如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];

問題在於,當您將其滾動出視圖然后又返回視圖時,該單元將被刷新(或重新使用,因此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的類,它具有兩個屬性text和isChecked。 加載視圖時,使用以前使用的文本用這些對象填充NSMutableArray(listData)。 當您的單元格被選中時,當單元格被重用時,將isChecked屬性設置為YES,該屬性將其狀態保留在委托方法之外,並且應正確呈現。

將完整的代碼放在方括號中:

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

這絕對應該工作。

暫無
暫無

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

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