1

I am developing iOS App with Multiple TableViews on Single View controller. The Value show on each tableview from a single array. But the problem is that the array value is displayed on first table view but not on the other. Some time second tableview cell value show First tableview. I also tried to do with tableview tag value.

 code..

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [_arrayResult 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 ( tableView == _tableView) { //_clentSelect.tag == 2

            MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
            UILabel *dropValue = (UILabel *)[cell viewWithTag:100];
            dropValue.text = obj.emp_client_name;
        }
        if (tableView == _eventSeclect) {  //_clentSelect.tag == 2

            MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
            UILabel *dropValue = (UILabel *)[cell viewWithTag:1000];
            dropValue.text = obj.emp_event_name;
            NSLog(@"%@",dropValue.text);
        }
    //    if(_clentSelect.tag == 3) {
    //      
    //        MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
    //        cell.textLabel.text = obj.emp_client_name;
    //    }
        return cell;
    }
2
  • You have to check the tableView before giving it value...eg. if tableView == tableOne Commented Sep 7, 2017 at 8:20
  • your array is same then your cell value also tha same with indexpath in all table Commented Sep 7, 2017 at 9:14

3 Answers 3

2
        - (void)viewDidLoad {
            [super viewDidLoad];
           //reload your table 
        }

    #pragma mark - UITableViewDataSource
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (tableView.tag==1) {

            return  1;
        }if (tableView.tag==2) {
        return  1;
        }
        else
        {
        return 0;
        }
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView.tag == 1) {
            return  10;
        }if (tableView.tag == 2) {
        returns 20;
        }
        else{return 0;}
    }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (tableView.tag == 1) {
        static NSString *cellIdentifier = @“tbl1”;
        cell = [_tbl  dequeueReusableCellWithIdentifier:cellIdentifier];
        UILabel *title = (UILabel *)[cell viewWithTag:30];
        title.text = @"1245";
             return cell;
    }
    if (tableView.tag == 2) {

    static NSString *cellIdentifier = @“tbl2”;
        cell = [_tbl1 dequeueReusableCellWithIdentifier:cellIdentifier];
        UILabel *title = (UILabel *)[cell viewWithTag:30];
        title.text = @"1245";
        return cell;    
}
Sign up to request clarification or add additional context in comments.

Comments

0

Is this working for you? If not, also try changing UITableView outlet name.

if ( tableView == tableViewFirst) { //_clentSelect.tag == 2

        MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
        UILabel *dropValue = (UILabel *)[cell viewWithTag:100];
        dropValue.text = obj.emp_client_name;
    }
else {  //_clentSelect.tag == 2

        MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
        UILabel *dropValue = (UILabel *)[cell viewWithTag:1000];
        dropValue.text = obj.emp_event_name;
        NSLog(@"%@",dropValue.text);
    }

Comments

0

You always get a reference and check for which tableView delegate or dataSource method is called i.e. in case you need different tableview with different array then you need to set number of rows and section for each of your tableview, like this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView1)
    {
        return 1;
    }

    if (tableView == self.tableView2)
    {
        return 1;
    }

    if (tableView == self.tableView3)
    {
        return 1;
    }
}

Also, for number of rows,

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

    if (tableView == self.tableView1)
    {
        return 1;
    }

    if (tableView == self.tableView2)
    {
        return 2;
    }

    if (tableView == self.tableView3)
    {
        return 3;
    }
}

Check which tableview you are giving which value,

You can custom cell for both tableview or create separate cell for each tableview.

  1. Same cell for both tableview: (register your custom nib in view did load)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (tableView == myTableView1) {
            // your code 1
        }
        else 
        if (tableView == myTableView2) {
            // your code 2
        }
        else 
        if (tableView == myTableView3) {
            // your code 3
        }
    }
    
  2. Different cell for tableview: (register your custom nib in view did load)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == myTableView1) {
            // your code 1
            NSString *CellIdentifier = @"cell1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
        else 
        if (tableView == myTableView2) {
            // your code 2
            NSString *CellIdentifier = @"cell2";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
        else 
        if (tableView == myTableView3) {
            // your code 3
            NSString *CellIdentifier = @"cell3";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.