【问题标题】:Issue with setting icon image in cell在单元格中设置图标图像的问题
【发布时间】:2011-06-06 04:29:40
【问题描述】:

我正在尝试在表格视图的每个单元格中创建一个“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 正在其他单元格中重复使用。我怎样才能避免这种情况发生?

【问题讨论】:

  • 如果你问iphone相关的问题,请贴上iphone标签,我相信它比ios或objective-c更容易到达。然后更多的人会看到你的问题..
  • @Krishnabhadra:[iPhone] 标签应该用于only 应用于 iPhone 的问题。这个问题是关于iOS的,并且使用了Objective-C;这些标签是完全合适的,而 [iPhone] 则不合适。

标签: objective-c ios uitableview uiimage reuseidentifier


【解决方案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"];
    }
    ...
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • Whit,它可以工作,但我最初的“实心星”也被替换为再次成为空白星。如何存储星形图标的状态?
    • 您需要在 NSMutableArray 或其他东西中从外部跟踪这一点。细胞的工作方式非常棘手。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多