Ich entwickle eine App, die zwei uitables
in der gleichen View
benötigt, also ziehe ich zwei tableViews
in die .xib Datei und füge den folgenden Code hinzu.Second tableView lädt keine Daten
@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;
}
}
die App läuft, wird numberOfRowsInSection
weder cellForRowAtIndexPath
genannt werden immer nur tableView1
mit Daten nach [self.tableView1 reloadData];
auch numberOfRowsInSection
und cellForRowAtIndexPath
gefüllt sind berufen, aber tableView2
immer leer bleibt, wird [self.tableView2 reloadData];
nicht in diesem tableView2
arbeiten.
Basierend auf dem obigen Beispiel, wie kann ich Werte in beiden Tabellen anzeigen? Fehlt etwas?
Haben Sie Ihre Klasse die 'dataSource' und' delegate' sowohl Ihre 'tableViews' gemacht? – Adeel
@Adeel Ja hat er, in 'viewDidLoad'. – NobodyNada
Wenn Sie 'if (tableView == self.tableView1)' in 'if (tableView == self.tableView2)', in 'tableView: cellForRowAtIndexPath' und' tableView: numberOfRowsInSection: 'ändern, tausch die Tabellen den Inhalt (also' tableView1 'ist leer und' tableView2' ist ausgefüllt. – NobodyNada