1

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

-(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 中,否则不会在任何表中加载任何数据。

4

2 回答 2

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];
    }
}
于 2013-09-10T12:54:35.143 回答
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;
}
于 2013-09-10T13:30:23.340 回答