Multiple UITableview in Single Viewcontroller

后端 未结 5 1891
無奈伤痛
無奈伤痛 2020-12-09 10:16

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 delegat

相关标签:
5条回答
  • 2020-12-09 11:05

    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
      }
    }
    
    0 讨论(0)
  • 2020-12-09 11:07

    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;
        }   
    }
    
    0 讨论(0)
  • 2020-12-09 11:07

    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
    }
    
    0 讨论(0)
  • 2020-12-09 11:14
    //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;
    }
    
    
    }
    
    0 讨论(0)
  • 2020-12-09 11:16

    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];
    
    0 讨论(0)
提交回复
热议问题