我有一个按钮,在tableviewcell检测哪个按钮需要修改文本在细胞
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
DBImageView *imageView = [[DBImageView alloc] initWithFrame:(CGRect){ 0, 0, 320, 320 }];
[imageView setPlaceHolder:[UIImage imageNamed:@"Placeholder"]];
[imageView setTag:101];
[cell.contentView addSubview:imageView];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
[(DBImageView *)[cell viewWithTag:101] setImageWithPath:[tmpDict objectForKey:@"thumb_link"]];
likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
[likeButton setFrame:CGRectMake(cell.frame.size.width-60,280,81,33)];
likeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
likeButton.contentEdgeInsets = UIEdgeInsetsMake(3, 2, 0, 0);
[likeButton.titleLabel setFont:[UIFont fontWithName:@"Trebuchet MS" size:16]];
[likeButton setBackgroundImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:@"heart_pressed.png"] forState:UIControlStateSelected];
[cell addSubview:likeButton];
// like button
likeButton.selected = ![likeButton isSelected];
NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];
if (defaultKey2 == 0)
{
//[likeButton setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
[likeButton setSelected:NO];
}
else if (defaultKey2 == 1)
{
//[likeButton setTitleColor:[UIColor colorWithRed:229/255.0 green:85/255.0 blue:140/255.0 alpha:1.0] forState:UIControlStateNormal];
[likeButton setSelected:YES];
}
[likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal];
[likeButton addTarget:self action:@selector(like:) forControlEvents: UIControlEventTouchUpInside];
[likeButton setTag:indexPath.row];
return cell;
}
这是我的行动:
-(void)like:(id) sender{
UIButton *likebtn = (UIButton *)sender;
NSDictionary *tmpDict = myObject[likebtn.tag];
NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];
if (defaultKey2 == 0)
{
//Increase value
int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1;
NSString *strValue = [NSString stringWithFormat:@"%d", varTemp];
[likeButton setTitle:strValue forState:UIControlStateNormal];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:1 forKey:[tmpDict objectForKey:@"id"]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
if (defaultKey2 == 1)
{
//Decrease value
int varTemp = [[tmpDict objectForKey:@"likes"] intValue]-1;
NSString *strValue = [NSString stringWithFormat:@"%d", varTemp];
[likeButton setTitle:strValue forState:UIControlStateNormal];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:0 forKey:[tmpDict objectForKey:@"id"]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:likebtn.tag inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationNone];
}
当我尝试在细胞 设置标题为我的按钮[likeButton setTitle:strValue forState:UIControlStateNormal];
值错误的单元格。
如何检测单元值中的哪个按钮需要设置?我如何使用我的标签?
你是什么意思“价值错误细胞”?你可以请你发布你的代码从cellForRowAtIndexPath? – 2014-12-01 16:27:58
我需要增加或减少喜欢的值,当我按下按钮时,值错误的单元格更改。 – 2014-12-01 16:29:48
好吧,有几件事情......在你的'like:'方法里,“likeButton”来自条件之内吗?因为你在方法开始时声明了“likebtn”。二,你不能这样设置按钮标题。它必须在cellForRowAtIndexPath中设置。一旦你解释了“likeButton”的声明,我会很快发布一个建议。 – 2014-12-01 16:41:11