1

我正在尝试在表格视图的每个单元格中创建一个“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”的单元格,星形将按预期变为实心。

在此处输入图像描述

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

在此处输入图像描述

任何人都可以帮助解释为什么会这样吗?似乎state该单元格的 正在被其他单元格重新使用。我怎样才能避免这种情况发生?

4

2 回答 2

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

    }
于 2011-06-06T04:36:15.013 回答
1

您可以将按钮的状态存储在 NSMutableArray 中,并在绘制单元格时根据 NSMutableArray 设置是否启用或禁用。要更改数组上的值,您应该标记单元格并在 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"];
    }
    ...
}
于 2011-06-06T05:08:20.073 回答