2017-06-15 17 views
0

我必須在一個視圖控制器中調用兩個tableviews。我爲第一個tableview分配了標記值,我只獲取了一個tableview的數據,其他未顯示任何數據。我怎樣才能得到兩個tableviews的數據。如何在一個視圖控制器中爲tableview分配標記值

我已經指派firsttableview.tag=1

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(tableView.tag==1){ 
        FirstTableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:@"ProfileTableViewCell"]; 

     if(!cell1){ 
      cell1=[[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"FirstTableViewCell"]; 
     } 
     cell1.backgroundColor=[UIColor clearColor]; 
     [email protected]"ios"; 
     [email protected]"9:30AM"; 
     //[self.secondTableView reloadData]; 
     return cell1; 

    } 
      SecondTableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:@"SecondTableViewCell"]; 

     if(!cell1){ 
      cell1=[[SecondTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"SecondTableViewCell"]; 
     } 
     cell1.backgroundColor=[UIColor clearColor]; 
     [email protected]"Hyderabad"; 
      return cell1; 
    } 
    } 
+0

你的代碼在哪裏?請發佈'cellForRowAtIndexPath'方法的正確代碼,併發布numberOfRows方法代碼。 – CodeChanger

回答

0

儘量不使用tag財產。相反,有更好的方法。

  1. 您連接兩個UITableView s與您的控制器。

    @property (weak, nonatomic) IBOutlet UITableView *tableViewA; 
    @property (weak, nonatomic) IBOutlet UITableView *tableViewB; 
    
  2. viewDidLoad()

    self.tableViewA.dataSource = self; 
    self.tableViewB.dataSource = self; 
    
  3. 處理您DataSource方法

    然後據此

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
        if (tableView == self.tableViewA) { 
         return 1; //return according to your need 
        } else if (tableView == self.tableViewB) { 
         return 1; //return according to your need 
        } 
        return 0; 
    } 
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
        if (tableView == self.tableViewA) { 
         return 5; //return according to your need 
        }else if (tableView == self.tableViewB) { 
         return 8; //return according to your need 
        } 
        return 0; 
    } 
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        UITableViewCell *cell = [[UITableViewCell alloc]init]; 
        if (tableView == self.tableViewA) { 
         cell = [tableView dequeueReusableCellWithIdentifier:@"CellA" forIndexPath:indexPath]; 
         cell.textLabel.text = @"Type A"; 
        } else if (tableView == self.tableViewB) { 
         cell = [tableView dequeueReusableCellWithIdentifier:@"CellB" forIndexPath:indexPath]; 
         cell.textLabel.text = @"Type B"; 
        } 
        return cell; 
    } 
    
1

使用他們的對象,如`檢查的tableview - (UITableViewCell的*)的tableView :(UITableView *)tableView cellForRowAtIndexPath:(NSI ndexPath *)indexPath {

如果([的tableView isEqual:方法表1]){

UITableViewCell *cell1 = [[UITbaleViewCell alloc] init]; 

return cell1 

}

否則如果([的tableView isEqual:方法表2]){

UITableViewCell *cell2 = [[UITbaleViewCell alloc] init]; 

return cell2 

} 

} `

相關問題