【发布时间】: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