如何在普通 UIViewController 中使用 UITableView
1个回答
展开全部
运行到
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个委托就会崩溃,请问是哪里出问题了???
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) NSArray * dataProvide;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
_dataProvide =[NSArray arrayWithObjects:@{@"icon": @"SOE_government",@"title": @"SOE and government"},@{@"icon": @"company_employee",@"title": @"Company employee"},@{@"icon": @"high_level_manager",@"title": @"High level manger"},@{@"icon": @"SME_owner",@"title": @"SME owner"},@{@"icon": @"sales",@"title": @"Sales"},@{@"icon": @"liberal",@"title": @"Liberal professions"}, nil];
CGRect gridCGRect = CGRectMake(10, 60, self.view.frame.size.width-20, self.view.frame.size.height-60);
UITableView *grid = [[UITableView alloc]initWithFrame:gridCGRect];
UITableViewCell *gridCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
[grid addSubview:gridCell];
[self.view addSubview:grid];
grid.delegate = self;
grid.dataSource = self;
}
#pragma mark -tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataProvide count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
UITableViewCell *cell;
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSDictionary *celldb = [self.dataProvide objectAtIndex:indexPath.row];
cell.imageView.image = [celldb objectForKey:@"icon"];
cell.textLabel.text = [celldb objectForKey:@"title"];
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个委托就会崩溃,请问是哪里出问题了???
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) NSArray * dataProvide;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
_dataProvide =[NSArray arrayWithObjects:@{@"icon": @"SOE_government",@"title": @"SOE and government"},@{@"icon": @"company_employee",@"title": @"Company employee"},@{@"icon": @"high_level_manager",@"title": @"High level manger"},@{@"icon": @"SME_owner",@"title": @"SME owner"},@{@"icon": @"sales",@"title": @"Sales"},@{@"icon": @"liberal",@"title": @"Liberal professions"}, nil];
CGRect gridCGRect = CGRectMake(10, 60, self.view.frame.size.width-20, self.view.frame.size.height-60);
UITableView *grid = [[UITableView alloc]initWithFrame:gridCGRect];
UITableViewCell *gridCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
[grid addSubview:gridCell];
[self.view addSubview:grid];
grid.delegate = self;
grid.dataSource = self;
}
#pragma mark -tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataProvide count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
UITableViewCell *cell;
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSDictionary *celldb = [self.dataProvide objectAtIndex:indexPath.row];
cell.imageView.image = [celldb objectForKey:@"icon"];
cell.textLabel.text = [celldb objectForKey:@"title"];
}
return cell;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询