Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 滚动期间设置UITableViewCell按钮图像值_Iphone_Objective C_Uitableview_Uibutton - Fatal编程技术网

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

Iphone 滚动期间设置UITableViewCell按钮图像值,iphone,objective-c,uitableview,uibutton,Iphone,Objective C,Uitableview,Uibutton,我遇到一个问题,我试图保存一个重复图像是否显示选中(它是在IB中创建的UITableViewCell上创建的UIButton) 问题是,由于单元格被随机重复使用,图像在开始滚动后会被重置或设置在其他地方。我看了所有的地方,建议是设置一个NSMutableArray来存储按钮的选择状态,我正在一个名为checkRepeatState 我的问题是:如果返回视图的给定单元格的checkRepeatState设置为0或1,我应该将if-then-else语句放在代码中的什么位置,使其实际更改按钮图像?现

我遇到一个问题,我试图保存一个重复图像是否显示选中(它是在IB中创建的
UITableViewCell
上创建的
UIButton

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

我的问题是:如果返回视图的给定单元格的
checkRepeatState
设置为0或1,我应该将if-then-else语句放在代码中的什么位置,使其实际更改按钮图像?现在,我使用的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;
}

您是否也将按钮状态设置为“已选定”?如果您只希望按钮显示不同的图像,请使用
uicontrolstatemormal
。如果您同时设置了普通图像和选定图像,则只需使用
repeatbutton.selected=YES

设置选定状态即可。您只想在drawnonward的帮助下共享工作代码。。再次感谢!希望其他人能从这个代码中得到帮助

- (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;
}
我的repeat button函数被调用:

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

}

我在iAction中为另一个函数中的其他位置的按钮设置该值(我还在可变数组中设置了一个选定值)。这与UITableViewCell的重用有关,而与按钮状态是否选中无关。当表格视图单元格进入视图时,无论选择状态如何,图像都是随机的repeat.png或repeatselected.png,这取决于单元格是否从另一个单元格重新使用。您应该能够在nib中设置正常和选定的图像,只需在
tableView:cellforrowatinexpath:
中设置所选状态,或者忽略所选状态并始终将图像设置为正常状态。由于您没有还原选定的状态,因此两个图像都可能显示。明白了,我在viewDidLoad中设置了不同的图像,效果很好。但是,当我尝试设置“选定”或“未选定”时,它会根据重复使用tableview单元格的方式错误地显示图像。NSLogs正确地告诉我它是否为选定状态。我真正的问题是,如果他们在表格视图中选择了许多“重复”按钮中的一个,然后向下滚动到视图外,重复按钮图像就不会保持不变,因为单元格重复使用。这正是我想弄明白的。如何重置单元格或使其在单元格返回视图后正确设置图像。您的提示让我明白了这一点!它工作!!!谢天谢地。。。我会发布下面的工作代码,并给你信用,再次感谢!
- (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];
}       

}