我有一個按鈕,在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