【发布时间】:2016-11-07 17:01:09
【问题描述】:
我正在开发一个需要两个uitables 在同一个View 中的应用程序,所以我将两个tableViews 拖到.xib 文件并添加以下代码。
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (weak, nonatomic) IBOutlet UITableView *tableView2;
@implementation SimpleTableViewController
{
NSArray *tableData1;
NSArray *tableData2;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView1.dataSource = self;
self.tableView1.delegate = self;
self.tableView2.dataSource = self;
self.tableView2.delegate = self;
// Initialize table data
tableData1 = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
tableData2 = [NSArray arrayWithObjects:@"Egg Benedict2", @"1", @"2", @"3", @"4", @"5", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView1) {
return [tableData1 count];
}
else {
return [tableData2 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tableView1) {
//...code...
return cell;
}
else {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData2 objectAtIndex:indexPath.row];
return cell;
}
}
运行应用程序,numberOfRowsInSection 和 cellForRowAtIndexPath 都没有被调用,只是 tableView1 被数据填充后
[self.tableView1 reloadData]; 还调用了 numberOfRowsInSection 和 cellForRowAtIndexPath,但 tableView2 始终为空,[self.tableView2 reloadData]; 在此 tableView2 中不起作用。
根据上面的例子,如何在两个表中显示值?有什么遗漏吗?
【问题讨论】:
-
你的班级是
dataSource和delegate的tableViews吗? -
@Adeel 是的,他有,在
viewDidLoad。 -
如果您将
if (tableView == self.tableView1)更改为if (tableView == self.tableView2),在tableView:cellForRowAtIndexPath和tableView:numberOfRowsInSection:中,表格是否交换内容(所以tableView1为空白且tableView2已填充)?跨度> -
是的,表格交换了内容,但内容只显示在
tableView1,tableView2继续为空 -
确保您的表格链接到 IB 中的 ivars。
标签: ios objective-c uitableview