繁体   English   中英

在预填充的表视图上显示NSArray

[英]Displaying an NSArray on a pre-populated table view

因此,对于我的应用程序,我有一个文本字段和一个搜索按钮。 用户输入用户名,我希望它显示在“ usersTable”上。 这是该代码:

- (IBAction)searchButtonPressed:(id)sender {
    NSLog(@"search button pressed");
    NSString *searchedText = _searchField.text;
    //Queries for users.
    PFQuery *filterQuery = [PFQuery queryWithClassName:@"_User"];
    [filterQuery setLimit:1000];
    [filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (!error) {
            // Here you can store fetched data of parse to your array.
            NSArray *filteredData;
            filteredData = [NSArray arrayWithArray:objects];

        }
    }];
     NSArray *filteredData;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchedText];
     NSArray *newArray = [filteredData filteredArrayUsingPredicate:predicate];



}

但是,我完全不知道这是否可以用作过滤系统,以及如何填充数组NSArray的UITableView的“ usersTable”。 该表已被填充,我想删除已经存在的所有内容并在NSArray newArray中显示结果。 有人可以批准此代码并告诉我如何删除,然后填充新的数组吗?

谢谢你的帮助

正确的方法是使用UISearchController,下面是一个示例: http : //www.jhof.me/simple-uisearchcontroller-implementation/

一种快速的方法是创建一个BOOl属性,该属性将在您搜索/不搜索时更改。 在tableview数据源委托方法中,您可以基于此值查询正确的数组。

您可以使用NSArray填充“ usersTable”(即UITableView)。 您的userTable(我们称其为myArray)可以是该类的@property:

@interface ViewController()
@property (nonatomic) NSMutableArray *myArray;
@end

然后,为了使用此数组中的数据填充tableView,请确保在tableView委托方法中使用该数组。 例如 :

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [self.myArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.myArray objectAtIndex:indexPath.row]];
    return cell;
}

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM