如何用代码在UIViewController 中添加UITableView
推荐于2018-04-24
展开全部
运行到
- (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;
}
资料来源:cocoachina问答。
- (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;
}
资料来源:cocoachina问答。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
运行到
- (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;
}
资料来源:cocoachina问答
- (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;
}
资料来源:cocoachina问答
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询