2016-11-03 4 views
0

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?

+0

Haben Sie Ihre Klasse die 'dataSource' und' delegate' sowohl Ihre 'tableViews' gemacht? – Adeel

+0

@Adeel Ja hat er, in 'viewDidLoad'. – NobodyNada

+0

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

Antwort

-1

Ich glaube, Sie die Delegaten für die Tableview einstellen verpasst 2.

Sie die Delegaten in xib festlegen kann, indem Besitzer Datei ist oder Sie es in viewDidLoad wie

self.tableView2.delegate = self; 
+0

Nein, er legt die Datenquelle und den Delegaten in 'viewDidLoad' fest. – NobodyNada

0

festlegen In tableView:cellForRowAtIndexPath und tableView:numberOfRowsInSection Richter die tableview mag:

- (NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == tableView1) { 
    return 1; 
    } else { 
    return 1; 
    } 
} 
-1

Haben Sie heightForRowAtIndexPath eingestellt?

+0

können Sie im Kommentarbereich fragen. Dies ist nicht die Art zu antworten. –

0

Ich denke, dieser Code nützlich ist für Sie

@interface ViewController()<UITableViewDelegate,UITableViewDataSource> 
{ 
    NSArray *tableData1; 
    NSArray *tableData2; 
} 
@end 
@implementation ViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    tableview1.dataSource = self; 
    tableview1.delegate = self; 

    tableview2.dataSource = self; 
    tableview2.delegate = self; 
    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.tag == 1) { 
     return [tableData1 count]; 
    } 
    else { 
     return [tableData2 count]; 
    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView.tag == 1) { 
     static NSString *simpleTableIdentifier = @"cell1"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
     } 

     cell.textLabel.text = [tableData1 objectAtIndex:indexPath.row]; 
     return cell; 
    } 
    else { 
     static NSString *simpleTableIdentifier = @"cell2"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
     } 

     cell.textLabel.text = [tableData2 objectAtIndex:indexPath.row]; 
     return cell; 

    } 
}