17

I have a viewcontroller in that i want to show 3 tableviews(because the content and the table properties are different). How do i add these delegate methodes for 3 tables in one viewcontroller?

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array1 count];
}

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

EDIT

So what will i do if i want add a uislider to one table row using custom cell and when i slide the value i want to change the display brightness?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _displayThemes) {
        return 1;
    } else {
        return 1; 
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView==_displayThemes) {
        return 1;
    } else {
        return 5;
    }
}

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

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == _displayThemes) {
        cell.textLabel.text = [displaytheme objectAtIndex:indexPath.row];
        return cell;
    } else {
        cell.textLabel.text = [fontlist objectAtIndex:indexPath.row];
        return cell;
    }
}


- (IBAction)fontButton:(id)sender {
    _fontList = [[UITableView alloc]init];
    [self.view addSubview:_fontList];
    [UIView animateWithDuration:1.5
            delay:0
            options: UIViewAnimationOptionTransitionCurlUp
            animations:^{
                _fontList.fram e= CGRectMake(0,800,320,200);
            }
            completion:^(BOOL finished){
                _fontList.frame = CGRectMake(0,280,320,200);     
    }];

    [_fontList reloadData];
}

- (IBAction)button:(id)sender {
    _displayThemes = [[UITableView alloc]init];
    [self.view addSubview:_displayThemes];
    [UIView animateWithDuration:1.5
            delay:0
            options: UIViewAnimationOptionTransitionCurlUp
            animations:^{
                _displayThemes.frame=CGRectMake(0,800,320,200);
            }
            completion:^(BOOL finished){
                _displayThemes.frame=CGRectMake(0,280,320,200);
    }];
}
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
Naveen
  • 1,251
  • 9
  • 20
  • 38

5 Answers5

34

It will be the same as you do it with one table view, but you should check which tableview is currently using.

myTableView1.dataSource = self;
...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (tableView == myTableView1) {
    // your code 1
  }
  else 
  if (tableView == myTableView2) {
      // your code 2
  }
  else 
  if (tableView == myTableView3) {
      // your code 3
  }
}

Edit:

About brightness:

How to change brightness in iOS 5 app?

And about UISlider it has minimunValue and maximumValue properties.

- (void) sliderChanged:(UISlider*)sender{
    UISlider *slider = (UISlider*)sender;
    [[UIScreen mainScreen] setBrightness:slider.value];
}

Edit:

slider.tag = 1;
[cell addSubview:slider];


...
// when you need..
indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion];
UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109
  • 1
    can i add static NSString *CellIdentifier=@"Cell"; common for all? – Naveen Apr 24 '13 at 15:19
  • I think that yes, but i remember that NSString *CellIdentifier = [NSString stringWithFormat: @"row%dsection%d", indexPath.row, indexPath.section]; worked for me. – B.S. Apr 24 '13 at 15:21
  • i apreciate ur answer but how do i get this value from tableview row? – Naveen Apr 24 '13 at 15:40
6

You always get a reference and can always check for which tableView delegate or dataSource method is called.

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

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

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

You don't gain anything by using same identifier for all tables. Use something like:

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }

        cell.textLabel.text = [NSString stringWithFormat: @"table1: %d.%d", indexPath.section, indexPath.row];

        return cell;
    }

    if (tableView == self.tableView2)
    {
        static NSString *CellIdentifier2 = @"cellForTable2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
        }

        cell.textLabel.text = [NSString stringWithFormat: @"table2: %d.%d", indexPath.section, indexPath.row];

        return cell;
    }

   if (tableView == self.tableView1)
    {
        static NSString *CellIdentifier3 = @"cellForTable3";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];

        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
        }

        cell.textLabel.text = [NSString stringWithFormat: @"table3: %d.%d", indexPath.section, indexPath.row];

        return cell;
    }   
}
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • can i add static NSString *CellIdentifier=@"Cell"; common for all? – Naveen Apr 24 '13 at 15:19
  • If all the cells in all the tables are of same type then you can. But i'd separate the reusing functionality with distinct cell identifiers. – Rok Jarc Apr 24 '13 at 15:37
  • My table is not displaying anything now – Naveen Apr 24 '13 at 15:39
  • Can you post your new code - the one that isn't working. Not all the code, just table delegate and dataSource methods... – Rok Jarc Apr 24 '13 at 15:45
  • Try modifying your cellForRowAtIndexPath to suit the format in my answer. As for slider in a custom cell you should post another question - it's completely different topic. – Rok Jarc Apr 24 '13 at 15:50
  • @RokJarc what about height.when i use two tableview.height is not working – Krutarth Patel Oct 14 '16 at 06:18
  • @KrutarthPatel: hmm, can't say anything based on your comment alone. did you open a question with a bit of more details? if so you can post a link here and i'll take a look at it – Rok Jarc Oct 14 '16 at 07:13
  • @RokJarc thanks for quick response.http://stackoverflow.com/questions/40020007/how-to-create-sections-in-cell-in-table-view?noredirect=1#comment67317597_40020007 – Krutarth Patel Oct 14 '16 at 07:15
4
//add tag in tableView .
myTable1.tag = 200;
myTable2.tag = 201;
myTable3.tag = 202;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 200)
{
    return 1;
}
if (tableView.tag == 201)
{
    return 1;
}
if (tableView.tag == 202)
{
    return 1;
}


}
3

You can manage multiple tableView in a single ViewController by writing below code inside UItableViewDelegate and UItableViewDatasource.

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView  == tableView1
    {
        // place your code here
    }
    else if tableView  == tableView2 {
        // place your code here
    }
    else {
      return 0
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView  == tableView1
    {
        // place your code here
    }
    else if tableView  == tableView2 {
        // place your code here
    } 
    else {
        return 0
    }
}
// You can set a different size of your tableView using below lines of code 
if tableView  == tableView1{
    return 50
}
else{
    return 40
}
Parth Patel
  • 859
  • 2
  • 11
  • 28
Praful Argiddi
  • 1,152
  • 1
  • 8
  • 12
  • Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Jun 22 '18 at 07:21
1

None of the previous worked for me, I come up with the following solution:

  public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  if((tableView1 != nil) && (tableView  == tableView1)) {
   return Items1.count
  }
  else if((tableView2 != nil) && (tableView  == tableView2)) {
   return Items2.count
  }
  else if((tableView3 != nil) && (tableView  == tableView3)) {
   return Items3.count
  }
  else {
   return 0
  }
}
R Suteu
  • 11
  • 1