如果在一个ViewController中使用两个UITableView,而另一个使用自定义单元格refrence,而另一个则很简单

我想在一个ViewController中使用两个UITableView。 一个UITableView与自定义单元格的引用,另一个是简单的…我已经写了这段代码,但它给了我错误的控制可能会达到非无效function的结束… … –

所以给我这个build议…我能做些什么?

提前致谢…

这里是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView==self.categoryTable) { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell1) { cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } cell1.textLabel.text=[category objectAtIndex:indexPath.row]; cell1.textLabel.highlightedTextColor = [UIColor redColor]; return cell1; } else if (tableView==self.listTable) { static NSString *simpleTableIdentifier = @"SimpleTableCell"; RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell2 == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell2 = [nib objectAtIndex:0]; } //NSLog(@"%ld",(long)indexxxx); //NSLog(@"%lu",(unsigned long)imagesmarry.count ); cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row]; //NSLog(@"%@",[imagesmarry_pictitle objectAtIndex:indexxxx]); return cell2; } } 

尝试这一点,你必须初始化单元格为UITableviewViewCell并投在之后。 告诉我,如果这个样本回答你的问题。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (tableView==self.categoryTable) { static NSString *cellIdentifier = @"Cell"; cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } cell.textLabel.text=[category objectAtIndex:indexPath.row]; cell.textLabel.highlightedTextColor = [UIColor redColor]; } else if (tableView==self.listTable) { static NSString *simpleTableIdentifier = @"SimpleTableCell"; cell = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell = (RWTListCell *)[nib objectAtIndex:0]; } //NSLog(@"%ld",(long)indexxxx); //NSLog(@"%lu",(unsigned long)imagesmarry.count ); ((RWTListCell *) cell).textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row]; //NSLog(@"%@",[imagesmarry_pictitle objectAtIndex:indexxxx]); } return cell; } 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if (tableView==self.categoryTable) { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell1) { cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } cell1.textLabel.text=[category objectAtIndex:indexPath.row]; cell1.textLabel.highlightedTextColor = [UIColor redColor]; cell=cell1; } else if (tableView==self.listTable) { static NSString *simpleTableIdentifier = @"SimpleTableCell"; RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell2 == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell2 = [nib objectAtIndex:0]; } //NSLog(@"%ld",(long)indexxxx); //NSLog(@"%lu",(unsigned long)imagesmarry.count ); cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row]; //NSLog(@"%@",[imagesmarry_pictitle objectAtIndex:indexxxx]); cell=cell2; } return cell; } 

你可以失去第二个条件

 if (tableView==self.categoryTable) { //do stuff return cell1; } else { //do stuff return cell2; } 
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; if (tableView==self.categoryTable) { ... ... return cell1; } else if (tableView==self.listTable) { ... ... return cell2; } retrun cell; } 

试试这个,拿一个虚拟单元格,并在最后返回。

其实你的问题是没有任何单元格返回到tableview

我已经改变了你的代码。 我只是删除你的else if条件

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView==self.categoryTable) { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell1) { cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } cell1.textLabel.text=[category objectAtIndex:indexPath.row]; cell1.textLabel.highlightedTextColor = [UIColor redColor]; return cell1; } static NSString *simpleTableIdentifier = @"SimpleTableCell"; RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell2 == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell2 = [nib objectAtIndex:0]; } //NSLog(@"%ld",(long)indexxxx); //NSLog(@"%lu",(unsigned long)imagesmarry.count ); cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row]; //NSLog(@"%@",[imagesmarry_pictitle objectAtIndex:indexxxx]); return cell2; } 

只需添加回报; 在最后。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (condition 1) { // statements return cellType_1; } else if (condition 2) { //statements return cellType_2; } return; } 

或者你也可以这样做 –

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (condition 1) { // statements return cellType_1; } else { //statements return cellType_2; } } 

删除第二个条件。 因为你必须在无效的方法天气中返回你的所有条件是真实的。