【发布时间】:2014-09-18 14:51:11
【问题描述】:
我和我的 TableView Cell Custom 有问题。
我声明我正在使用 Parse.com 来保存数据。
简而言之,我有一个带有自定义单元格的表格视图,其中包含一个按钮...
这个按钮的动作被分成两个代表
- (void) addFriendUserButtonPressed: (UITableViewCell *) customCell;
- (void) removeFriendFromList: (UITableViewCell *) customCell;
第一种方法将一些数据保存到数据库中,第二种方法删除它们
用户单击第一个单元格的按钮并更改按钮上的图像... 滚动 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;
}
【问题讨论】:
-
哪一行改变了你所说的图像?
-
对不起,我的大脑现在完全死了 :( 图像没有改变是自定义单元格中更改的按钮的色调...原谅我
-
你得到这个结果是因为细胞重用。您不应更改 cellForRowAtIndexPath(或 willDisplayCell:forRowAtIndexPath:)之外的单元格(标签文本、颜色、图像等)中任何 UI 元素的状态。当您滚动时,该单元格将被重用,如果您更改单元格类中的状态,就像您所做的那样,该更改将显示在重用该单元格的任何位置。
-
Rdelmar 你好,谢谢你的建议......事实上,以这种方式工作有点复杂,但现在我创造了整个东西,扔掉一切重新开始将是一场噩梦...我必须对自定义单元格进行这些更改,因为我创建了滑动单元格...此时我该如何修复它?
-
没有办法使用您当前的方法来修复它。您需要跟踪数据源中的任何状态(单元 UI 元素)——这就是表格视图的工作方式。
标签: ios tags uibutton parse-platform