如何在一个视图控制器中为tableviews分配标记值

时间:2017-06-15 06:13:15

标签: ios objective-c

我必须在一个视图控制器中调用两个tableview。我为第一个tableview分配了标记值,我只获取了一个tableview的数据,其他没有显示任何数据。如何获取两个tableviews的数据

我已分配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];
        cell1.dateLbl.text=@"ios";
        cell1.timeLbl.text=@"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];
        cell1.locationLbl.text=@"Hyderabad";
           return cell1;
   } 
   }

2 个答案:

答案 0 :(得分:1)

使用像` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

这样的对象检查tableview

if([tableView isEqual:table1]){

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

return cell1

}

else if([tableView isEqual:table2]){

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

return cell2

}

}`

答案 1 :(得分:0)

尽量不要使用tag属性。相反,有更好的方法。

  1. 您可以将两个UITableView与控制器连接。

    @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;
    }
    
相关问题