我在tableview中重用自定义单元格时出现问题。
我说的tableView有一个自定义单元格,里面有一个按钮,可以向该特定单元格的用户发送或取消朋友请求...
在这个TableView中我还有一个UISearchBar来过滤应用程序的用户..
在正常模式下重复使用单元格工作正常,但是当我去过滤器时,用户无法像正常模式那样维持按钮的状态......
示例..
TableView I搜索模式:
如果用户向Richard Marx发送请求按钮变成橙色正常流动,即使tableview到目前为止没有任何发生这么好......但如果你使用的UISearchBar与橙色按钮不匹配'包含理查德·马克思先生的单元格,但对于Abram Van Helsing先生来说,恰好是搜索模式列表中的第一个。
我已经创建了一个适合所点击用户的数组,但是如果你没有使用UISearchBar似乎只能工作 你能以任何方式帮助我吗?
这是用于过滤单元格中按钮状态的数组的实现
- (void)viewDidLoad {
[super viewDidLoad];
self.buttonPressedSelectedRowArray = [[NSMutableArray alloc] init];
int i = NO;
for (i=0; i<20; i++) {
[self.buttonPressedSelectedRowArray addObject:[NSNumber numberWithInt:NO]];
}
}
此方法插入到cellForRowAtIndexPath中我打开或关闭按钮
UIButton *myButton = (UIButton *)[cell.myContentView viewWithTag:100];
UIImage *imageFriendAdd = [UIImage imageNamed:@"On_userAddFriend@2x"];
if (myButton == nil) {
myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = CGRectMake(264.0, 30.0, 30.0, 30.0);
[myButton setBackgroundImage:imageFriendAdd forState:UIControlStateNormal];
myButton.tag = 100;
[myButton addTarget:self action:@selector(tapped:andEvent:) forControlEvents:UIControlEventTouchUpInside];
[cell.myContentView addSubview:myButton];
NSLog(@" BUTTON CREATED !!!");
} else {
}
if ([self.buttonPressedSelectedRowArray [indexPath.row] boolValue]) {
UIImage *imageFriendAddOn = [UIImage imageNamed:@"On_userAddFriend@2x"];
[myButton setBackgroundImage:imageFriendAddOn forState:UIControlStateNormal];
}
else {
UIImage *imageFriendAddOff = [UIImage imageNamed:@"Off_userAddFriend@2x"];
[myButton setBackgroundImage:imageFriendAddOff forState:UIControlStateNormal];
}
始终在cellForRowAtIndexPath中我使用布尔过滤用户进入搜索模式
PFObject *object = [userArray objectAtIndex:indexPath.row];
if (!isFiltered) {
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 *filteredObject = [filteredUserArray objectAtIndex:indexPath.row];
cell.user_photoProfile.file = [filteredObject 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 = [filteredObject objectForKey:NPUserKey_NOMECOGNOME];
cell.label_UserNomeCognome.text = str;
cell.itemText = str;
}
然而,这是UISearchBar过滤器的结构
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *) searchText{
if (searchText.length ==0){
isFiltered = NO;
}
else {
isFiltered = YES;
filteredUserArray=[[NSMutableArray alloc]init];
for(PFObject *object in self.userArray) {
NSString *str = [object objectForKey:NPUserKey_NOMECOGNOME];
NSRange stringRange =[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stringRange.location !=NSNotFound) {
[filteredUserArray addObject:object];
}
}
}
[tableViewFindUser reloadData];
}
还添加所选按钮的方法
-(void)tapped:(UIButton *)sender andEvent:(UIEvent *)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableViewFindUser];
NSIndexPath *indexPath = [self.tableViewFindUser indexPathForRowAtPoint:currentTouchPosition];
if ([self.buttonPressedSelectedRowArray[indexPath.row]boolValue]) {
[self.buttonPressedSelectedRowArray replaceObjectAtIndex:indexPath.row withObject:@(0)];
UIImage *imageFriendAddOff = [UIImage imageNamed:@"Off_userAddFriend@2x"];
[sender setBackgroundImage:imageFriendAddOff forState:UIControlStateNormal];
sender.transform = CGAffineTransformMakeScale(0.1,0.1);
[UIView beginAnimations:@"fadeInNewView" context:NULL];
[UIView setAnimationDuration:0.3];
sender.transform = CGAffineTransformMakeScale(1.0,1.0);
sender.alpha = 1.0f;
[UIView commitAnimations];
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]);
}
}];
} else {
[self.buttonPressedSelectedRowArray replaceObjectAtIndex:indexPath.row withObject:@(1)];
UIImage *imageFriendAddOn = [UIImage imageNamed:@"On_userAddFriend@2x"];
[sender setBackgroundImage:imageFriendAddOn forState:UIControlStateNormal];
sender.transform = CGAffineTransformMakeScale(0.1,0.1);
[UIView beginAnimations:@"fadeInNewView" context:NULL];
[UIView setAnimationDuration:0.3];
sender.transform = CGAffineTransformMakeScale(1.0,1.0);
sender.alpha = 1.0f;
[UIView commitAnimations];
PFObject *richiesta = [PFObject objectWithClassName:NPFriendClass];
if (!isFiltered) {
PFUser *userNotFiltered = [self.userArray objectAtIndex:indexPath.row];
[richiesta setObject:userNotFiltered forKey:NPFriend_AUser];
[richiesta setObject:userNotFiltered.objectId forKey:@"OBJECT_USER_ID"];
[richiesta setObject:userNotFiltered.username forKey:@"Username"];
[richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
[richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
[richiesta setValue:[NSNumber numberWithBool:YES] forKey:@"RECORD_INSERT"];
[richiesta saveInBackground];
NSLog(@"AMICO: %@", richiesta);
}
else {
PFUser *userFiltered = [self.filteredUserArray objectAtIndex:indexPath.row];
[richiesta setObject:userFiltered forKey:NPFriend_AUser];
[richiesta setObject:[PFUser currentUser] forKey:NPFriend_DaUser];
[richiesta setObject:@"Richiesta In Attesa" forKey:NPFriendRequestStatus];
[richiesta saveInBackground];
}
}
}