Подтвердить что ты не робот

Несколько UITableview в Single 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

Итак, что мне делать, если я хочу добавить 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);
    }];
}
4b9b3361

Ответ 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
  }
}

Изменить:

О яркости:

Как изменить яркость в приложении iOS 5?

И около 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];

Ответ 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;
    }   
}

Ответ 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

Ни один из предыдущих не работал у меня, я придумал следующее решение:

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