【问题标题】:Selection row on tableView iOStableView iOS上的选择行
【发布时间】:2014-12-08 00:48:54
【问题描述】:

我在 tableView 中选择一行时遇到问题,问题是当我第一次选择一行时,它会以灰色突出显示但没有任何反应,我必须再次选择一行来执行“didSelectRowAtIndexPath " 并带我到另一个 ViewController

// Number of rows in the tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
return 6;
}

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 175.0;
}

// Populating each cell at a time.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    ClientCustomCell *cell = (ClientCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ClientCustomCell" owner:self options:nil];
        cell = self.clientCustomCell;
        self.clientCustomCell = nil;

        cell.name = [nameArray objectAtIndex:indexPath.row];
        cell.number = [numberArray objectAtIndex:indexPath.row];
        cell.postal = [postalArray objectAtIndex:indexPath.row];
        cell.locality = [localityArray objectAtIndex:indexPath.row];
    }
    [cell setSelectionStyle:(UITableViewCellSelectionStyleBlue)];
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"row selecter %d", (int)indexPath.row);
    InfoViewController *info = [[InfoViewController alloc] init];
    [self.navigationController pushViewController:info animated:YES];
}

【问题讨论】:

  • 将 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 替换为 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

标签: ios objective-c uitableview selection


【解决方案1】:

您不小心实现了 DESELECTrowAtIndexPAth,而不是 SELECTrowAtIndexPath。

【讨论】:

    【解决方案2】:

    我发现了问题 你用didDeselectRowAtIndexPath方法代替didSelectRowAtIndexPath

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
    

    【讨论】:

      【解决方案3】:

      使用以下代码更新您的代码:

      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          [tableView deselectRowAtIndexPath:indexPath animated:YES];
          NSLog(@"row selecter %d", (int)indexPath.row);
          InfoViewController *info = [[InfoViewController alloc] init];
          [self.navigationController pushViewController:info animated:YES];
      }
      

      【讨论】:

        【解决方案4】:

        你已经实现了didDeselectRowAtIndexPath,这应该是didSelectRowAtIndexPath

        -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
        

        【讨论】:

          【解决方案5】:

          tableView iOS 上的选择行

          对于目标 c

             -(void)tableView:(UITableView *)tableView 
              didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
          

          适用于 Swift 4-5

           import UIKit
          
           extension UITableView {
          
          func deselectSelectedRow(animated: Bool)
          {
              if let indexPathForSelectedRow = self.indexPathForSelectedRow {
                  self.deselectRow(at: indexPathForSelectedRow, animated: animated)
              }
          }
          

          }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多