简体   繁体   中英

Using Multiple Tableview on single ViewController

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;
    }
        - (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;    
}

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);
    }

You always get a reference and check for which tableView delegate or dataSource method is called ie 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]; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM