-1

Want to use two tableView in one viewController but it give me error like:

this class is not key value coding-compliant for the key tableView.

I have two tableView 1.)tableViewTwo and 2.)tableViewThree but not wokring.

but when i used the name tableView then data shows on the first tableView.

and how can i use the different custom cell on both tableView. (CustomerListCell for (First TableView) and CustomerListCellThree for (Second TableView)

Can anyone please suggest me the best way to do this. I have search a lot and trying many suggestion which i found on internet and stackoverflow.

Thanks In Advanced.

- (void)viewDidLoad
{

    NSURLRequest *requestTwo=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xyzt.com?key=init"]];
    responseData=[[NSMutableData alloc]init];
    urlConnection=[[NSURLConnection alloc]initWithRequest:requestTwo delegate:self];


    NSURLRequest *requestThree=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://xyzt.com?key=init"]];
    responseDataThree=[[NSMutableData alloc]init];
    urlConnectionThree=[[NSURLConnection alloc]initWithRequest:requestThree delegate:self];



}


#pragma mark - Connection Details

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    if (connection == urlConnection) {
        NSLog(@"Error");
    }
    else if (connection == urlConnectionThree){
        NSLog(@"Error");
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    if (connection == urlConnection) {
        [responseData setLength:0];
    }
    else if (connection == urlConnectionThree){
        [responseDataThree setLength:0];
    }


}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    if (connection == urlConnection) {
        [responseData appendData:data];

    }
    else if (connection == urlConnectionThree) {
        [responseDataThree appendData:data];

    }

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    if (connection == urlConnection) {
        NSError *error=nil;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
        self.totalData=[dic objectForKey:@"data"];



        totalTitle=[[NSMutableArray alloc]init];
        totalImage=[[NSMutableArray alloc]init];
        totalIdWorks=[[NSMutableArray alloc]init];


        [_tableViewTwo reloadData];
    }
    else if (connection == urlConnectionThree){
        NSError *error=nil;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseDataThree options:NSJSONReadingMutableContainers error:&error];
        self.totalDataThree=[dic objectForKey:@"data"];


        totalTitleThree=[[NSMutableArray alloc]init];
        totalImageThree=[[NSMutableArray alloc]init];
        totalIdWorksThree=[[NSMutableArray alloc]init];


        [_tableViewThree reloadData];
    } 

}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.tableViewTwo) {
        return [totalData count];
    }

    else if (tableView == self.tableViewThree){
        return [totalDataThree count];
    }

    return 0;
}

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


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

    CustomerListCell *cell = (CustomerListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomerListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];



    }


    return cell;
}
user3171903
  • 23
  • 1
  • 4
  • 1
    You can access both TableView... by put condition like if(tableView == FirstTableView){....}else{....} – iPatel Jan 30 '14 at 05:16
  • In tableview delegate methods check the `tableView` is `tableViewTwo` or `tableViewThree`. And you can use different custom cells in `cellForRowAtIndexPath:` by the above check – Akhilrajtr Jan 30 '14 at 05:20
  • in interface builder you linked your IBOutlets from File's Owner when you should link them from the cell view. This might be the cause for getting the errors. – Kumar KL Jan 30 '14 at 05:29
  • @iPatel i want to shows data on both the tableView at same time so how would program knows data will shows on which tableView. if / else conditions works but how program knows which value is present in tableView currently for checking in if / else statement. – user3171903 Jan 30 '14 at 05:30

3 Answers3

1

Try this.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 if (tableView == tableViewFirst)
    return arrfirst.count;

else if(tableView==tableViewSecond)
    return arrsecond.count;

return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (tableView == tableViewfirst)
    {
       // First tableViewcell
       return cell;
     }
    else if (tableView == tableViewsecond)
    {
        //Second tableViewCell
      return cell;

    }
return nil;
}
Himanshu
  • 61
  • 4
  • i'm doing the same this but how the program knows the current value of tableView for matching the conditions in the if/else statement. both tableView are on the same view....How? – user3171903 Jan 30 '14 at 06:23
  • In cellForIndexpath your checking if tableView is first or second and if its first then first part will get executed and for second second part will. same as you are checking in all delegates. so the programme will understand which code should run. – Himanshu Jan 30 '14 at 06:53
0

No matter how much table view you have in a single viewController. As you said you have two tableView tableViewTwo & tableViewThree. So you have to create two array for two datasource. Then set the delegate of both table view.

[tableViewTwo setDelegate:self];
[tableViewThree setDelegate:self];

Now when you reload your table views

[tableViewTwo reloadData];
[tableViewThree reloadData];

for both table view the same delegate will called one after another. As you already put the condition on

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

do the same thing for all the delegate method you have implemented and you want to make differ for different table view. One thing I want to include use ([tableView isEqual:self.tableViewTwo]) instead (tableView == self.tableViewTwo)

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
  • refer to this link http://stackoverflow.com/questions/14036604/objective-c-nsobject-isequal-vs-comparison – Tapas Pal Jan 30 '14 at 06:58
  • No, in this case `==` is exactly what you want to do. You want to test for *which instance* of the table view the delegate/datasource is referring to. This is not a general test for equality. – trojanfoe Jan 30 '14 at 07:53
0

i think this is helpful you for understand @Tapas Pal said set delegates for both tableview

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
          NSString *customCell;
          UITableViewCell *customTabelCell;

            if(tableView == self.tableViewTwo){
              customCell=@"CustomerListCellTwo";
              customTabelCell=CustomerListTabelCellTwo;
            }else{
              customCell=@"CustomerListCellThree";
              customTabelCell=CustomerListTabelCellThree;
            }


        static NSString *simpleTableIdentifier = customCell;

        customTabelCell *cell = (customTabelCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:customCell owner:self options:nil];
            cell = [nib objectAtIndex:0];

        }


        return cell;
    }
codercat
  • 22,873
  • 9
  • 61
  • 85