我和我的TableView Cell Custom有问题。
我声明我正在使用Parse.com来保存数据。
简而言之,我有一个包含按钮的自定义单元格的表格视图......
此按钮的操作分为两个代表
第一种方法将一些数据保存到数据库,第二种方法删除它们
用户点击第一个单元格的按钮并更改按钮上的图像... 滚动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");
}
}
这是viewcontroller中包含tableView的两个方法。
-(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;
}
答案 0 :(得分:1)
正如rdelmar所说,你只能在cellForRowAtIndexPath
内更改UI。
你应该做的是给每个单元格一个变量(它可以是一个布尔值)。我们称之为颜色。单击该按钮时,找出所单击按钮的indexPath(请参阅here)并修改该indexPath的颜色变量(而不是按钮的tintColor)中的单元格。
然后在cellForRowAtIndexPath
中,获取当前indexPath的单元格。看看那个单元格的颜色变量。根据其值,相应地更改tintColor。