滚动期间设置UITableViewCell按钮图像值

时间:2010-05-09 03:07:20

标签: iphone objective-c uitableview uibutton

我遇到了一个问题,我在尝试保存重复图像是否会显示为已选中(在IB中创建的UIButton上创建为UITableViewCell)。

问题在于,由于单元格随机重复使用,因此在开始滚动后,图像会重置或设置在其他位置。我看了一遍,建议设置一个NSMutableArray来存储按钮的选择状态,我在一个名为checkRepeatState

的数组中这样做

我的问题是:我在哪里将if-then-else语句放在代码中,根据checkRepeatState是否为给定单元格设置为0或1来实际更改按钮图像的位置正在重新审视?现在,我正在使用的 if-then-else 语句在进入视图时对按钮图像完全没有影响。在这一点上我很困惑。

提前感谢您提供的任何见解!!!

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
// set up the cell

static NSString *CellIdentifier = @"PlayerCell";

PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {

    [[NSBundle mainBundle] loadNibNamed:@"PlayerNibCells" owner:self options:nil];
    cell = tmpCell;
    self.tmpCell = nil;

    NSLog(@"Creating a new cell");

}

// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
cell.useDarkBackground = (indexPath.row % 2 == 0);

// Configure the data for the cell.

NSDictionary *dataItem = [soundCategories objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [dataItem objectForKey:@"AnimalName"];

label = (UILabel *)[cell viewWithTag:2];    
label.text = [dataItem objectForKey:@"Description"];

UIImageView *img;
img = (UIImageView *)[cell viewWithTag:3];
img.image = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];

NSInteger row = indexPath.row;
NSNumber *checkValue = [checkRepeatState objectAtIndex:row];
NSLog(@"checkValue is %d", [checkValue intValue]);
// Reusing cell; make sure it has correct image based on checkRepeatState value
UIButton *repeatbutton = (UIButton *)[cell viewWithTag:4];

if ([checkValue intValue] == 1) {
    NSLog(@"repeatbutton is selected");
    [repeatbutton setImage:[UIImage imageNamed:@"repeatselected.png"] forState:UIControlStateSelected];
    [repeatbutton setNeedsDisplay];
} else {
    NSLog(@"repeatbutton is NOT selected");
    [repeatbutton setImage:[UIImage imageNamed:@"repeat.png"] forState:UIControlStateNormal];
    [repeatbutton setNeedsDisplay];
}   

cell.accessoryType = UITableViewCellAccessoryNone;

return cell;
}

2 个答案:

答案 0 :(得分:1)

[repeatbutton setImage:[UIImage imageNamed:@"repeatselected.png"] forState:UIControlStateSelected];

您是否也将按钮状态设置为选中状态?如果您只想让按钮显示不同的图像,请使用UIControlStateNormal。如果您同时设置了普通图像和所选图像,则只需使用repeatbutton.selected = YES设置所选状态。

答案 1 :(得分:0)

只是想在drawonward的帮助下分享工作代码..再次感谢!希望其他人可以通过此代码获得帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
// set up the cell
static NSString *CellIdentifier = @"PlayerCell";

PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {

    [[NSBundle mainBundle] loadNibNamed:@"PlayerNibCells" owner:self options:nil];
    cell = tmpCell;
    self.tmpCell = nil;

    NSLog(@"Creating a new cell");

            // this will setup the button images for each state initially to be changed later

    UIButton *repeatbutton;
    repeatbutton = (UIButton *)[cell viewWithTag:4];
    [repeatbutton setImage:[UIImage imageNamed:@"repeat.png"] forState:UIControlStateNormal];
    [repeatbutton setImage:[UIImage imageNamed:@"repeatselected.png"] forState:UIControlStateSelected];

}

// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
cell.useDarkBackground = (indexPath.row % 2 == 0);

// Configure the data for the cell... this gets called when the row scrolls into view each and every time

NSDictionary *dataItem = [soundCategories objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [dataItem objectForKey:@"AnimalName"];

label = (UILabel *)[cell viewWithTag:2];    
label.text = [dataItem objectForKey:@"Description"];

UIImageView *img;
img = (UIImageView *)[cell viewWithTag:3];
img.image = [UIImage imageNamed:[dataItem objectForKey:@"Icon"]];

NSInteger row = indexPath.row;
NSNumber *checkValue = [checkRepeatState objectAtIndex:row];
NSLog(@"checkValue is %d", [checkValue intValue]);
// Reusing cell; make sure it has correct image based on checkRepeatState value
UIButton *repeatbutton = (UIButton *)[cell viewWithTag:4];

if ([checkValue intValue] == 1) {
    NSLog(@"repeatbutton is selected");
    [repeatbutton setSelected:YES];
} else {
    NSLog(@"repeatbutton is NOT selected");
    [repeatbutton setSelected:NO];
}   

cell.accessoryType = UITableViewCellAccessoryNone;

return cell;
}

和我的重复按钮功能被调用:

- (void)repeatButton:(UIButton *)sender {

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSInteger row = indexPath.row;

// method for finding what button was pressed and changing the button image for repeat on/off

UITableViewCell *cell = (UITableViewCell*)[[sender superview] superview];

UIButton *repeatbutton;
repeatbutton = (UIButton *)[cell viewWithTag:4];

if ([sender isSelected]) {
    [checkRepeatState replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:NO]];
    [sender setSelected:NO];
} else {
    [checkRepeatState replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:YES]];
    [sender setSelected:YES];
}       

}