ios - UIButton 上的选择无法在我的带有 CustomCell 的 tableView 中工作

标签 ios tags uibutton parse-platform

我和我的 TableView Cell Custom 有问题。

我声明我正在使用 Parse.com 来保存数据。

简而言之,我有一个表格 View ,其中包含一个包含按钮的自定义单元格......

这个按钮的 Action 分为两个委托(delegate)

  • (void) addFriendUserButtonPressed: (UITableViewCell *) customCell;
  • (void) removeFriendFromList: (UITableViewCell *) customCell;

第一种方法将一些数据保存到数据库中,第二种方法删除它们

用户点击第一个cell的按钮,改变按钮上的图片... 滚动 tableView 我注意到其他未使用的按钮上的图像发生变化......自动将它们更改为全部......抱歉,如果我不能很好地表达自己,但他们花了 6 个小时来寻找解决方案。 ..

我确信使用标签可以解决,但我不知道使用哪种方法...我将发布我正在使用的代码

这是自定义单元格类中按钮的方法

-(IBAction)buttonClicked:(id)sender {
    if (sender == self.addUserButton) {


        if (!self.userSelected) {

            self.userSelected = YES;
            self.addUserButton.tintColor = [UIColor greenColor];
            [self.delegate addFriendUserButtonPressed:self];

        }

        else {

            self.userSelected = NO;
            self.addUserButton.tintColor = [UIColor redColor];

            [self.delegate removeFriendFromList:self];

        }





    }
else if (sender == self.seeProfileButton) {
        NSLog(@"Set  ");
        [self.delegate profileUserButtonPressed:self.itemText];

    } else if (sender == self.contactUserButton) {
        NSLog(@"CONTATTO  ");
        [self.delegate contactButtonPressed:self.itemText];

    }

    else {
        NSLog(@"Bottone Sconosciuto");
    }
}

这是包含 tableView 的 viewcontroller 中存在的两个方法。

-(void)addFriendUserButtonPressed:(UITableViewCell *)customCell  {


    NSIndexPath *indexPath = [self.tableViewFindUser indexPathForCell:customCell];

    PFObject *richiesta = [PFObject objectWithClassName:NPFriendClass];

    if (!isFiltered) {

        PFUser *userFiltered = [self.userArray objectAtIndex:indexPath.row];

        [richiesta setObject:userFiltered forKey:NPFriend_AUser];
        [richiesta setObject:userFiltered.objectId forKey:@"OBJECT_USER_ID"];
        [richiesta setObject:userFiltered.username forKey:@"Username"];
        [richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
        [richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
        [richiesta saveInBackground];

        NSLog(@"AMICO: %@", richiesta);
    }


    else {

        PFUser *userNotFiltered = [self.userFiltrati objectAtIndex:indexPath.row];

        [richiesta setObject:userNotFiltered forKey:NPFriend_AUser];
        [richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
        [richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
        [richiesta saveInBackground];

    }

}

-(void)removeFriendFromList:(UITableViewCell *)customCell {
    NSIndexPath *indexPath = [self.tableViewFindUser indexPathForCell:customCell];

    PFUser *userFiltered = [self.userArray objectAtIndex:indexPath.row];

    PFQuery *query = [PFQuery queryWithClassName:NPFriendClass];
    [query whereKey:NPFriend_DaUser equalTo:[PFUser currentUser]];
    [query whereKey:NPFriendRequestStatus equalTo:@"Richiesta In Attesa"];
    [query whereKey:@"OBJECT_USER_ID" equalTo:userFiltered.objectId];
    [query includeKey:NPFriend_AUser];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if(!error) {
            for (PFObject *object in objects) {
                NSLog(@"ELIMINATO: %@", object);
                [object deleteInBackground];
            }
        }

        else {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
    }];


}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {


    static NSString *CellIdentifier = @"cell";

    NPCustomFindUserCell *cell =[tableViewFindUser dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.delegate = self;


    if ([self.cellsCurrentlyEditing containsObject:indexPath]) {
        [cell openCell];
    }

    if (!isFiltered) {


        PFObject *object = [userArray  objectAtIndex:indexPath.row];

        cell.user_photoProfile.image = [UIImage imageNamed:@"camera"];
        cell.user_photoProfile.layer.masksToBounds = YES;
        cell.user_photoProfile.layer.cornerRadius = 3.0f;
        cell.user_photoProfile.contentMode = UIViewContentModeScaleAspectFill;
        cell.user_photoProfile.file = [object objectForKey:NPUserKey_PHOTOPROFILE];
        [cell.user_photoProfile loadInBackground];


        NSString *nameText = [object objectForKey:NPUserKey_NOMECOGNOME];
        cell.label_UserNomeCognome.text = nameText;
        cell.itemText = nameText;

    }

    else {

        PFObject *OggettiFiltrati = [userFiltrati objectAtIndex:indexPath.row];

             cell.user_photoProfile.file = [OggettiFiltrati objectForKey:NPUserKey_PHOTOPROFILE];
               cell.user_photoProfile.image = [UIImage imageNamed:@"camera"];
               [cell.user_photoProfile.layer setMasksToBounds:YES];
               [cell.user_photoProfile  .layer setCornerRadius:5.0f];
               cell.user_photoProfile.contentMode = UIViewContentModeScaleAspectFill;
               [cell.user_photoProfile loadInBackground];

        NSString *str = [OggettiFiltrati objectForKey:NPUserKey_NOMECOGNOME];
        cell.label_UserNomeCognome.text = str;
        cell.itemText = str;

    }

       return cell;
     }

最佳答案

正如 rdelmar 所说,您只能在 cellForRowAtIndexPath 中更改 UI。

你应该做的是给每个单元格一个变量(它可以是一个 bool 值)。让我们称之为颜色。单击按钮时,找出被单击按钮的 indexPath(请参阅 here)并修改该 indexPath 的颜色变量(而不是按钮的 tintColor)处的单元格。

然后在 cellForRowAtIndexPath 中,获取当前 indexPath 的单元格。查看该单元格的颜色变量。根据其值,相应地更改 tintColor。

关于ios - UIButton 上的选择无法在我的带有 CustomCell 的 tableView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915966/

相关文章:

ios - 将 UIPopover 正确放置在动态按钮旁边

ios - 是否可以更改具有不同类的对象的类类型?

ios - UIbutton标题行间距

ios - 将 16 位颜色范围内的值转换为 UIColor

ios - 如何以编程方式打开 UINavigationController

python - 使用 BeautifulSoup 解析标签

jquery链接标签启用禁用

html - 元标记:我使用名称还是ID?

objective-c - UIButton IB 与编程方式

ios - 带图像的自定义 UIAlertView