2014-04-13 32 views
0

如何在我的MasterViewController上加載2個tableViews?所述的tableView-方法我的代碼是:如何在一個MasterViewController上加載2個tableViews?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier1 = @"Cell"; 
    static NSString *CellIdentifier2 = @"Cell2"; 

    UITableViewCell *cell; 

    if(tableView == self.tableView) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier1]; 
     } 

     SKProduct * product = (SKProduct *) _products[indexPath.row]; 
     cell.textLabel.text = product.localizedTitle; 


    [_priceFormatter setLocale:product.priceLocale]; 
    cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price]; 

    if ([[FlamencompasIAPHelper sharedInstance] productPurchased:product.productIdentifier]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.accessoryView = nil; 
    } else { 
     UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     buyButton.frame = CGRectMake(0, 0, 72, 37); 
     [buyButton setTitle:@"Buy" forState:UIControlStateNormal]; 
     buyButton.tag = indexPath.row; 
     [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.accessoryView = buyButton; 
    } 
    } 
    //UITableView *tableView2; 
    if(tableView == self.tableView2) 
    //else 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier2]; 
     } 

     SKProduct * product = (SKProduct *) _products2[indexPath.row]; 
     cell.textLabel.text = product.localizedTitle; 


     [_priceFormatter setLocale:product.priceLocale]; 
     cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price]; 

     if ([[FlamencompasIAPHelper sharedInstance2] productPurchased:product.productIdentifier]) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      cell.accessoryView = nil; 
     } else { 
      UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      buyButton.frame = CGRectMake(0, 0, 72, 37); 
      [buyButton setTitle:@"Buy" forState:UIControlStateNormal]; 
      buyButton.tag = indexPath.row; 
      [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.accessoryView = buyButton; 
     } 
    } 
    return cell; 
} 

不執行if(tableView == self.tableView2)後的代碼。如何'聲明'tableView2? 我只能加載第一個或第二個tableView。但我想加載它們。這個怎麼做?

回答

0

你的方法應該是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.tableView) { 
     // All code related to self.tableView 
    } else if (tableview == self.tableView2) { 
     // All code related to self.tableView2 
    } else { 
     return nil; // oops 
    } 
} 

另外,還要確保self.tableViewself.tableView2設置正確。也許self.tableView2nil

+0

但是,在哪裏以及如何做我必須加載tableView2那麼你如何分開你的數據源? – user1189952

+0

與加載'self.tableView'的方式相同。你有一個'UIViewController',它有兩個表視圖,你的'tableView'和'tableView2'屬性被引用,對嗎?調用'[self.tableView reloadData]'重新加載'tableView'並調用'[self.tableView2 reloadData]'重新加載'tableView2'。 – rmaddy

+0

我MasterViewController.h樣子: – user1189952

0

我會創建2個tableview數據源類,以避免使您的主視圖控制器變得非常漫長而難以理解。如果你在同一個類中實現了UITableViewDataSource方法(即MasterViewController),那麼你將最終得到多個難以維護和重構的if/else語句。

此詳細介紹了從您的視圖控制器http://www.objc.io/issue-1/lighter-view-controllers.html

相關問題