分享

如何用代码在UIViewController 中添加UITableView

 没原创_去搜索 2015-07-09

如何用代码在UIViewController 中添加UITableView

 编程语言
专业回答
2015-06-26 17:03
运行到
- (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问答。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多