我有這樣的代碼:使一個UISearch酒吧iOS中
for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
我有過濾掉搜索的對象,當你點擊它把細胞上的複選標記搜索欄。但是,如果您搜索並單擊第一個單元格,它將檢查它。但是,如果刪除搜索文本,則複選標記將顯示在第一個單元格上,而不是我檢查的單元格。下面是一個視頻顯示這一點: https://docs.google.com/file/d/0B1Of4oDADQCQZkF0aGdhdmF0VjA/edit 我應該把上面的代碼下面的代碼,以便對號保持與選定單元格?
#import "MSAddFriendsViewController.h"
@interface MSAddFriendsViewController()
@end
@implementation MSAddFriendsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query = [PFUser query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@, %@", error, [error userInfo]);
}
else {
self.allUsers = objects;
[self.tableView reloadData];
}
}];
self.currentUser = [PFUser currentUser];
self.searchUsers.delegate = self;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
self.userResults = nil;
[self.tableView reloadData];
return ;
}
self.userResults = [NSMutableArray array];
for (PFUser *user in self.allUsers) {
if ([user.username rangeOfString:searchText
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)].location == 0) {
[self.userResults addObject:user];
}
}
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
if (self.userResults != nil) {
return [self.userResults count];
}
// Return the number of rows in the section.
return [self.allUsers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user;
if (self.userResults != nil) {
user = [self.userResults objectAtIndex:indexPath.row];
} else {
user = [self.allUsers objectAtIndex:indexPath.row];
}
cell.textLabel.text = user.username;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
} }];
}
@end
有人能改正上面的代碼嗎?
我是新來的目標c,所以你可以寫出代碼? – MicahKE
嘿,對不起,這是很多代碼寫出來的,我沒有時間爲你寫,但如果你花一些時間來制定我給你的指示,我保證你會走向正確的方向。 –
好的我得到了代碼 – MicahKE