我必须在一个视图控制器中调用两个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;
}
}
答案 0 :(得分:1)
使用像` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
这样的对象检查tableviewif([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
属性。相反,有更好的方法。
您可以将两个UITableView
与控制器连接。
@property (weak, nonatomic) IBOutlet UITableView *tableViewA;
@property (weak, nonatomic) IBOutlet UITableView *tableViewB;
然后在viewDidLoad()
self.tableViewA.dataSource = self;
self.tableViewB.dataSource = self;
相应地处理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;
}