【问题标题】:Multiple UITableview in Single Viewcontroller单个 Viewcontroller 中的多个 UITableview
【发布时间】:2013-04-18 05:17:54
【问题描述】:

我有一个viewcontroller,我想显示 3 个tableviews(因为内容和表格属性不同)。我如何在一个viewcontroller 中为 3 个表添加这些委托方法?

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

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

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

编辑

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

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

【问题讨论】:

标签: ios objective-c uitableview uiview uiviewcontroller


【解决方案1】:

这与您使用一个表格视图执行此操作相同,但您应该检查当前使用的是哪个表格视图。

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

编辑:

关于亮度:

How to change brightness in iOS 5 app?

关于UISlider,它具有minimunValue 和maximumValue 属性。

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

编辑:

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


...
// when you need..
indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion];
UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];

【讨论】:

  • 我可以添加静态 NSString *CellIdentifier=@"Cell";所有人都通用?
  • 我认为是的,但我记得 NSString *CellIdentifier = [NSString stringWithFormat: @"row%dsection%d", indexPath.row, indexPath.section];为我工作。
  • 我很欣赏你的答案,但我如何从 tableview 行获取这个值?
【解决方案2】:

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

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

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

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

对所有表使用相同的标识符不会有任何好处。使用类似的东西:

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

【讨论】:

  • 我可以添加静态 NSString *CellIdentifier=@"Cell";所有人都通用?
  • 如果所有表格中的所有单元格都属于同一类型,那么您可以。但我会将重用功能与不同的单元格标识符分开。
  • 我的表格现在没有显示任何内容
  • 你能发布你的新代码吗?那个不起作用的代码。不是所有的代码,只是表委托和数据源方法......
  • 尝试修改您的 cellForRowAtIndexPath 以适应我的答案中的格式。至于自定义单元格中的滑块,您应该发布另一个问题 - 这是完全不同的主题。
【解决方案3】:
//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;
}


}

【讨论】:

    【解决方案4】:

    您可以通过以下方式在单个 ViewController 中管理多个 tableView 在 UItableViewDelegate 和 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
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!请不要把你的源代码扔在这里。友善一点,并尝试对您的答案进行漂亮的描述,以便其他人会喜欢并支持它。见:How do I write a good answer?
    【解决方案5】:

    以前的方法都不适合我,我想出了以下解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      相关资源
      最近更新 更多