在单元格中设置图标图像的问题

时间:2011-06-06 04:29:40

标签: objective-c ios uitableview uiimage reuseidentifier

我正在尝试在表格视图的每个单元格中创建一个“blank_star图标”,在用户点击它之后,该星标应该会成为一个“实心”星标。

我创建了UIButton的子类,如下所示

//.h file
@interface Bleh : UIButton {

}

+(id)specialInit;
-(void)vvv;

@end

//.m file
@implementation Bleh

+(id) specialInit
{
    Bleh* button=[super buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"blank_star.png"] forState:UIControlStateNormal];    
    [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateDisabled];    
    [button addTarget:button action:@selector(vvv)    forControlEvents:UIControlEventTouchUpInside];    
    NSLog(@"%d",[button isEnabled]);
    return button;
}


-(void)vvv
{
    NSLog(@"button tapped");
    [self setEnabled:false];
}

@end

我在表格视图的cellforRow:方法中添加了UIButton的子类,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    int row = indexPath.row;
    NSString *cc = [array objectAtIndex:row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        button.frame = CGRectMake(0, 0, 100, 100);
        NSLog(@"Button:%@ at row number: %i",button, indexPath.row);

        cell.textLabel.text = cc;
        [cell.contentView addSubview:button];

    }
    return cell;
}

但是我在运行应用时遇到了问题。例如,如果我点击标记为“a”的单元格,则该星形将按预期变为实体。

enter image description here

奇怪的是,在向下滚动之后,我还会看到其他一些带有实心星的细胞(参见单元格'e')。

enter image description here

任何人都可以帮忙解释为什么会这样吗?似乎细胞的state正在其他细胞中重复使用。我怎样才能避免这种情况发生?

2 个答案:

答案 0 :(得分:1)

重复使用细胞。我自己也遇到过这个问题。 这可能会占用更多内存,因为它不会从内存中删除旧单元,但它确实使编码更简单。 一个诀窍是这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    NSString *CellIdentifier = [NSString stringWithFormat@"Cell %i", row];
    NSString *cc = [array objectAtIndex:row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        // Configure the cell...    
        Bleh *button = [Bleh specialInit];
        button.frame = CGRectMake(0, 0, 100, 100);
        NSLog(@"Button:%@ at row number: %i",button, indexPath.row);

        cell.textLabel.text = cc;
        [cell.contentView addSubview:button];

    }

答案 1 :(得分:1)

您可以将按钮的状态存储在NSMutableArray中,并在绘制单元格时根据NSMutableArray启用或禁用它。要更改Array上的值,您应该标记单元格并在vvv选择器上进行更改。

//.h file
@interface Bleh : UIButton {
    NSMutableArray *data;
}

关于你的功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    // Configure the cell...    
    Bleh *button = [Bleh specialInit];
    [button setTag:row] // row is the id of the button
    if([data objectAtIndex:row] isEqualToString:@"ENABLED"]) [button setEnabled:TRUE];
    else [button setEnabled:FALSE];
    ...
}

在您的vvv选择器

-(void)vvv:(id)sender {
    if([sender isEnabled]) {
        [sender setEnabled:FALSE];
        [data replaceObjectAtIndex:[sender tag] withObject:@"DISABLED"];
    }
    else {
        [sender setEnabled:TRUE];
        [data replaceObjectAtIndex:[sender tag] withObject:@"ENABLED"];

    }
}

你应该在viewDidLoad上初始化数组,比方说10个单元格

- (void)viewDidLoad
{
    ...
    data = [[NSMutableArray alloc] initWithCapacity:10];
    for ( int i = 0; i < 10; i++ ) {
        [data addObject:@"ENABLED"];
    }
    ...
}