专业回答 运行到 - (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问答。 |
|