将数据源分配给一个视图控制器中的两个表视图

时间:2013-09-10 12:52:36

标签: iphone ios uitableview

以下代码有什么问题? 在这里,我使用两个表视图并为它们分配不同的数据源,但它不起作用

-(void)viewDidLoad
{
    [super viewDidLoad];

    arryData = [[NSArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
    arryData2 = [[NSArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];

    flag=1;
    myTable1.hidden=YES;
    myTable2.hidden=YES;
    btnOne.layer.cornerRadius=8;
    btnTwo.layer.cornerRadius=8;
    myTable1.layer.cornerRadius=8;
    myTable2.layer.cornerRadius=8;
    myTable1.delegate=self;
    myTable1.dataSource=self;
    myTable2.delegate=self;
    myTable2.dataSource=self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     
{
    if(self.myTable1=tableView)
    {
        return [arryData count];     
    }
    else {
        return [arryData2 count];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    if(tableView==self.myTable1)
    {
        static NSString *CellIdentifier1 = @"Cell1";

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

        cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
        NSLog(@"1here is%i  %@",indexPath.row,cell.textLabel.text);
         return cell;
    }
    else 
    {
        static NSString *CellIdentifier2 = @"Cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) 
       {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
        }

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


-(IBAction)btnCategory:(id)sender 
{
    if (flag==1) {
        flag=0;
        myTable1.hidden=NO;
        myTable2.hidden=YES;

    }
    else{
        flag=1;
        myTable1.hidden=YES;
        myTable2.hidden=YES;

    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    myTable1.hidden=YES;
    myTable2.hidden=YES;
}

-(IBAction)btnTopic:(id)sender 
{
    if (flag==1) {
        flag=0;
        myTable2.hidden=NO;
        myTable1.hidden=YES;    
    }
    else{
        flag=1;
        myTable2.hidden=YES;
        myTable1.hidden=YES;

    }    
}
</code>

当我删除mytable2的数据源时,所有数据都被添加到table1中,否则没有任何内容被加载到表中。

2 个答案:

答案 0 :(得分:4)

你错了。看我的代码..

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{
    if(self.myTable1==tableView) // ----- Change here you need to write == symbol instead of = symbol
    {
        return [arryData count];        
    } else {
        return [arryData2 count];
    }
}

答案 1 :(得分:2)

试试这个,

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==myTable1)
{
    return [arryData count];
}
else {
    return [arryData2 count];
}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if(myTable1==tableView)
{

    cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
    NSLog(@"1here is%i  %@",indexPath.row,cell.textLabel.text);
}
else
{
    cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
}
return cell;
}