ios 单个 ViewController 中的多个 UITableview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16195660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:19:55  来源:igfitidea点击:

Multiple UITableview in Single Viewcontroller

iosobjective-cuitableviewuiviewuiviewcontroller

提问by Naveen

I have a viewcontrollerin 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?

我有一个viewcontroller我想显示 3 tableviews(因为内容和表格属性不同)。如何将 3 个表的这些委托方法合二为一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 uisliderto one table row using custom celland when i slide the value i want to change the display brightness?

那么,如果我想uislider使用自定义将一个表格行添加到一个表格行cell并且当我滑动该值时我想更改显示亮度,我该怎么办?

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

回答by B.S.

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?

如何在 iOS 5 应用程序中更改亮度?

And about UISliderit has minimunValueand maximumValueproperties.

关于UISlider它有minimunValuemaximumValue属性。

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

回答by Rok Jarc

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

你总是得到一个引用,并且总是可以检查调用了哪个 tableView 委托或 dataSource 方法。

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

回答by Kishor Kumar Rawat

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


}

回答by praful argiddi

You can manage multiple tableViewin a single ViewControllerby writing below code inside UItableViewDelegateand UItableViewDatasource.

通过在UItableViewDelegateUItableViewDatasource 中编写以下代码,您可以在单个ViewController 中管理多个tableView

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
}

回答by R Suteu

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