【问题标题】:iOS, How to find selected row Of UITableView?iOS,如何找到 UITableView 的选定行?
【发布时间】:2013-09-05 13:12:51
【问题描述】:

我正在尝试查找选定的UITableViewCell,如下所示:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 

       reuseIdentifier:CellIdentifier];

  }
    if   ([indexPath row] == [tableView cellForRowAtIndexPath:indexPath.row]) // i want to place value of selected row to populate the buttons 

        //([indexPath row] == 0)

        //(indexPath.row == ![self cellIsSelected:indexPath]) 

    {
        UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image

        [AddComment addTarget:self 
                       action:@selector(TestBtns:)
             forControlEvents:UIControlEventTouchDown];

        // [AddComment setTitle:@"1" forState:UIControlStateNormal];

        AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);

        [cell.contentView addSubview:AddComment];

        UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"];

        [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];

        [cell.contentView addSubview:AddComment]; 


    }

如何实现这一点,请您指导我,我在哪里做错了。

【问题讨论】:

  • didselectrowatindexpath 方法在哪里???
  • 你想在哪里细化 UITableview 的选定单元格..?在 didselectrowatindexpath 还是按钮 TouchUiInside 方法?
  • 请同时检查“if([indexPath row] == [tableView cellForRowAtIndexPath:indexPath])”这一行。在这里,您将 NSInteger 与 UITableViewCell 进行比较!

标签: iphone ios objective-c uitableview


【解决方案1】:

目标 C

 NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

Swift 代码

let indexPathForSelectedRow = self.tableView.indexPathForSelectedRow

【讨论】:

  • 你为什么不接受这个作为正确答案? @pratap Manish Bhadoria
  • Methinks @PratapManishBhadoria 忘了检查这个答案。
  • @TaimurAjmal 你能具体一点吗?正如你从投票中看到的那样
  • @SurajKThomas,哦,是的,它有效,是问题的正确答案。
【解决方案2】:

使用UITableView的这个委托方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@"%d", indexPath.row); // you can see selected row number in your console;
}

【讨论】:

    【解决方案3】:

    在 UITableViewDataSource 委托中有一个方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    它返回包含所选部分和所选行的 NSIndexPath 对象。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"Selected section>> %d",indexPath.section);
        NSLog(@"Selected row of section >> %d",indexPath.row);
    }
    

    使用前一定要设置tableview的数据源,否则这个方法不会被调用

    【讨论】:

      【解决方案4】:
      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          int a = indexPath.row;
          NSLog(@"index: %i",a);
      }
      

      【讨论】:

        【解决方案5】:

        我认为您要做的不是获取单击的表格视图单元格的索引,而是知道单击了哪个单元格。

        要在分配每个单元格时知道这一点,请将 indexpath.row 值分配为创建的单元格的标签,然后单击将表格视图作为父视图并尝试获取其子视图(单元格)带有该标签

        [带有标签的表格视图视图:indexpath.row]

        【讨论】:

          【解决方案6】:

          这是帮助您确定选择了哪个单元格的内置方法。

          - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
          {
              // you can use "indexPath" to know what cell has been selected as the following
              NSLog(@"Selected row is %@", indexPath);
          }
          

          顺便说一句,当你创建 TableViewController 时,方法已经给出,你可能只需要取消注释它。

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 2013-03-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多