【问题标题】:Create multiple column table with search filter function使用搜索过滤功能创建多列表
【发布时间】:2016-02-23 16:25:05
【问题描述】:

1.我需要在我的视图控制器中创建一个多列表来显示 json 数据。我在 youtube 上学习教程,但那些表视图教程只有单列。

2.这是我的单列表格视图代码。

#import "inventoryVC.h"
@interface inventoryVC () <UIApplicationDelegate>
@property (nonatomic,strong) NSArray *inventoryarray;
@property (strong, nonatomic)  NSArray *searchresult;
@end

@implementation inventoryVC

@synthesize tableview = tableview;
- (void)viewDidLoad {
[super viewDidLoad];
self.inventoryarray =[[NSArray alloc] initWithObjects: @"first item" , @"second item", @"third item", @"fourth item", @"fifth item", @"sixth item", @"seventh item" ,nil];
self.searchresult =[[NSArray alloc]init];


// Do any additional setup after loading the view, typically from a nib.
}

#pragma table View methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [self.searchresult count];
}
else {
    return [self.inventoryarray count];
}

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *simpleTableIdentifier = @"CellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [self.searchresult objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [self.inventoryarray objectAtIndex:indexPath.row];
}

return cell;

}

#pragma search methods

-(void) filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

self.searchresult = [self.inventoryarray filteredArrayUsingPredicate:resultPredicate];

[self.searchDisplayController.searchResultsTableView reloadData];
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString  {

[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]] ;

 return YES;
}

3.我创建了一个名为inventoryarray 的数组来保存要在表格视图中显示的虚拟数据。我可以将json数据放入数组中吗?

【问题讨论】:

  • 我建议您在获得反对票之前搜索“[ios] [UITableView] 多列”。
  • 您必须为每一行创建一个自定义的 uitableviewcell 并在该自定义单元格中设置标签和按钮。看看这个问题/答案stackoverflow.com/questions/2855857/…
  • 在我的表格视图中创建自定义单元格后,我需要做的就是告诉 xcode 哪个 json 数据要显示在哪个标签上? @yan

标签: objective-c json tableview


【解决方案1】:

我想最好的方法是创建一个自定义 UITableViewCell 并在其中设置标签和列分隔符。

【讨论】:

  • 谢谢你的建议,我试试看。
  • 顺便问一下,如何添加列分隔符?其实我还没有理解它的概念。
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 2012-05-05
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多