aca*_*era 3 uibutton uitableview ios
我有一个iPhone应用程序问题一直困扰我几天,它似乎不应该是这么困难,所以我敢肯定我错过了一些明显的东西.我已经研究过很多关于"类似"主题的论坛讨论,但实际上并没有解决这个问题.
需要说明的是,如果有一些文档或其他来源我应该研究,请指出正确的方向.
开始...
我有一个项目列表,我在表格中显示给用户(uitableview).每个项目的单元格(uitableviewcell)是自定义的,包含图像和Like按钮(uibutton).正如所料,对于表中的每个项目,用户可以单击"赞"按钮或忽略它.like按钮调用单独的进程来更新服务器.简单吧?
所以,这是问题:
当在特定单元格上单击"赞"按钮时,"选定"状态正常工作,但是当您将单元格滚出视图时,表格中的其他随机单元格将"相似"按钮显示为"已选择",即使它们从未被触摸过.因为细胞被重复使用,出于明显的性能原因,我理解为什么会发生这种情况.我不明白的是为什么我的方法(见下面的代码)不会像我认为的那样覆盖或重置按钮的状态.为简洁起见,我只在此处包含相关代码(希望格式正确):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCustomCell";
MyCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCustomViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *myRating = [[self.dataArray objectAtIndex:indexPath.row] valueForKey:@"my_rating"];
// Create the Like button
UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(260, 68, 40, 40)];
[likeButton setImage:[UIImage imageNamed:@"thumbsUp"] forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:@"thumbsUpSelected"] forState:UIControlStateSelected];
if (myRating == @"9") {
[likeButton setSelected:YES];
}
[likeButton setTitle:@"9" forState:UIControlStateNormal];
[likeButton setTag:indexPath.row];
[cell.contentView addSubview:likeButton];
[likeButton addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)likeButtonPressed:(UIButton *)sender {
// Changed the Selected state on the button
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
// Create a new object with the user's rating and then replace it in the dataArray
NSString *ratingText = sender.titleLabel.text;
NSMutableArray *myMutableArray = [[self.dataArray objectAtIndex:row] mutableCopy];
[myMutableArray setValue:ratingText forKey:@"my_rating"];
[self.dataArray replaceObjectAtIndex:row withObject:myMutableArray];
}
Run Code Online (Sandbox Code Playgroud)
所以,我已经经历了很多迭代,但我似乎无法获得按钮的状态来显示所选项目的所选项目,并保留那些尚未被喜欢的项目的正常图像.
任何帮助或建议将不胜感激.
有一个简单的方法.你可以欺骗按钮以保持最新的选择状态.
创建一个可变数组,以保持按钮的选定状态
selectedButton = [[NSMutableArray alloc]init];//i've already defined the array at the .h file
for (int i = 0; i<yourTableSize; i++) //yourTableSize = how many rows u got
{
[selectedButton addObject:@"NO"];
}
Run Code Online (Sandbox Code Playgroud)
在tableviewcell方法中,声明你的按钮并设置它以便它引用mutablearray来设置它的选择状态
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
UIImage *img = [UIImage imageNamed:@"btn_unselected.png"];
UIButton *toggleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[toggleButton setImage:img forState:UIControlStateNormal];
img = [UIImage imageNamed:@"btn_selected.png"];
[toggleButton setImage:img forState:UIControlStateSelected];
[toggleButton setTag:indexPath.row+100];//set the tag whichever way you wanted it, i set it this way so that the button will have tags that is corresponding with the table's indexpath.row
[toggleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:toggleButton];
//and now we set the button's selected state, everytime the table reuse/redraw the cell the button will set it's selected state according to the array
if([[selectedButton objectAtIndex:indexPath.row]isEqualToString:@"NO"])
{
[toggleButton setSelected:NO];
}
else
{
[toggleButton setSelected:YES];
}
return cell;
Run Code Online (Sandbox Code Playgroud)
最后,创建按下按钮时按钮触发的方法,以更改其选定状态
-(void)buttonPressed:(UIButton*)sender
{
int x = sender.tag - 100; //get the table's row
if([sender isSelected]) //if the button is selected, deselect it, and then replace the "YES" in the array with "NO"
{
[selectedButton replaceObjectAtIndex:x withObject:@"NO"];
[sender setSelected:NO];
}
else if (![sender isSelected]) //if the button is unselected, select it, and then replace the "NO" in the array with "YES"
{
[selectedButton replaceObjectAtIndex:x withObject:@"YES"];
[sender setSelected:YES];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8099 次 |
| 最近记录: |