[ios]在哪个按钮上检测到需要更改单元格中的文本

标签: Xcode ios UIButton
发布时间: 2017/3/28 23:41:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有喜欢的 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]; 值去错误的单元格。

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

解决方法 1:

您不能设置按钮标题,这样在你 like: 方法,即︰

[likeButton setTitle:strValue forState:UIControlStateNormal];

(1) 因为你要重新加载表右之后因此手动触发调用 cellForRowAtIndexPath: 和 (2) 即使你没有重新加载表数据,甚至滚动会触发调用 cellForRowAtIndexPath ,因此你的细胞会填充基于 cellForRowAtIndexPath: 的信息。

就现在看,你 cellForRowAtIndexPath: 的方法使用下面的一行,设置标题︰

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

这意味着,为了为 like: 到触发标题变化的行动,你必须改变中的 [tmpDict objectForKey:@"likes"] 值 like: 。因为您正在使用一个临时的字典和永远不会回到 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) 不要那样做。将它保留在本地,ex:

UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ;

(2) 你重用你的细胞,但每次添加一个新按钮。所以你要添加一个按钮,再在另一个与每个 cellForRowAtIndexPath: 调用。为你前进,这可能导致问题。

赞助商