gpt4 book ai didi

ios - 检测哪个按钮需要更改单元格中的文本

转载 作者:行者123 更新时间:2023-11-29 02:28:10 24 4
gpt4 key购买 nike

我在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]; 值转到错误的单元格。

如何检测需要设置单元格值中的哪个按钮?我如何使用我的标签?

最佳答案

您不能在 like: 方法中以这种方式设置按钮标题,即:

[likeButton setTitle:strValue forState:UIControlStateNormal];

(1) 因为您随后要重新加载表格,因此手动触发对 cellForRowAtIndexPath: 的调用,并且 (2) 即使您没有重新加载表格数据,即使滚动也会触发调用 cellForRowAtIndexPath,因此您的单元格将根据 cellForRowAtIndexPath: 的信息填充。

就目前而言,您的 cellForRowAtIndexPath: 方法使用以下行来设置标题:

[likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal];

这意味着为了让 like: 操作触发标题更改,您必须更改 like: 中的 [tmpDict objectForKey:@"likes"] 值>。由于您使用的是临时字典并且从未将字典添加回 myObject 数组,因此您没有更改最终决定 cellForRowAtIndexPath: 中按钮标题的值。所以你必须相应地更新 myObject 。这是我的代码建议:

-(void)like:(id)sender {

UIButton *likebtn = (UIButton *)sender;
NSMutableDictionary *tmpDict = myObject[likebtn.tag];
NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]];

if (defaultKey2 == 0)
{
//Increase value
int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1;

// Then update myObject
[tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"];
[myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict];

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;

// Then update myObject
[tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"];
[myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict];


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

}

编辑:还有一个大问题,因为我已经看到了您的 cellForRowAtIndexPath: 方法。您已将按钮声明为类变量? (1) 不要那样做。保持在本地,例如:

UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ;

(2) 您正在重复使用您的单元格,但每次都添加一个新按钮。因此,每次调用 cellForRowAtIndexPath: 时,您都会在另一个按钮之上添加一个按钮。这可能会给您的前进带来麻烦。

关于ios - 检测哪个按钮需要更改单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27232697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com