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数据放在数组中吗?
答案 0 :(得分:0)
我想最好的方法是创建一个自定义的UITableViewCell,然后在其中划分标签和列分隔符。