如何使用多个cell.textLabel.text进行多个tableview?

时间:2015-08-31 04:49:50

标签: ios objective-c uitableview

我正在尝试将两个tableviews合并为一个UIViewController,在这里,我创建了两个tableviewIf conditional statement来为单独的表操作单独的进程。但问题是我需要为单独的separate cell.textLabel.text创建tables。每当我点击单元格时,我都会获得第一个Tableview cell.textLabel.text值。

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

    if (tableView==tableone)
    {
        static NSString *cellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell.textLabel.text = [....]; // Here I am applying first table values 
    }
    if (tableView==tabletwo) {

        static NSString *cellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell.textLabel.text = [....]; // here I am applying my values 
    }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

   if (tableView==tableone) {

      UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
       cellText = selectedCell.textLabel.text;
       NSLog(@"%@",cellText);
   }

  if (tableView==tabletwo) {

      UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
       cellText = selectedCell.textLabel.text;
       NSLog(@"%@",cellText);
   }

}

1 个答案:

答案 0 :(得分:0)

    You can give tag of the tableview in utility view.
    after giving tag you can use tableview using given particular tag.

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

    if (tableView.tag == 1)
    {
        static NSString *cellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell.textLabel.text = [....]; 
    }
    if (tableView.tag ==2) {

        static NSString *cellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell.textLabel.text = [....];
    }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

   if (tableView.tag == 1) {

      UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
       cellText = selectedCell.textLabel.text;
       NSLog(@"%@",cellText);
   }

  if (tableView.tag == 2) {

      UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
       cellText = selectedCell.textLabel.text;
       NSLog(@"%@",cellText);
   }
}

//我认为这会对你有帮助..