【问题标题】:ios - how do I detect if and which item was tapped in a list of items in a UITableViewios - 我如何检测是否以及在 UITableView 的项目列表中点击了哪个项目
【发布时间】:2012-12-14 22:28:32
【问题描述】:

我有一个填充列表的文本项列表。它的代码如下所示:

// CREATING EACH CELL IN THE LIST
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"business";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    }



    cell.textLabel.text = [cellTitleArray objectAtIndex:indexPath.row];


    // CLOSE THE SPINNER
    [spinner stopAnimating];

    // return the cell for the table view
    return cell;
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17];
    CGSize constraintSize = CGSizeMake(self.itemList.frame.size.width, MAXFLOAT);

    CGSize labelSize = [[cellTitleArray objectAtIndex:indexPath.row] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 30;
}


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

}

我想做的是让这个人选择编辑每个项目。一种有意义的方法是将其设置为 2 列列表,并在右侧列中创建一个类似“>”的字符,这样他们就可以点击它,它会给他们一个编辑项目的选项。

在这种情况下怎么做?

谢谢!

【问题讨论】:

  • 嗯...当点击一行时,didSelectRowAtIndexPath 被调用,所以只需要让用户在那里编辑该行吗?查看表格视图的编辑模式。或者你想要一个自定义的编辑视图(比如另一个编辑屏幕)
  • @yuf 我想给他们一个选项,比如“你确定要编辑这个部分吗?”如果他们说是,我会把他们带到屏幕上。那是笨拙的用户体验吗?

标签: ios uitableview ios5


【解决方案1】:

要捕获一行点击:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Use indexPath.row to find your selection
    ...
}

如果您只观察一个按钮或类似的点击:

[self.buttono addTarget:self action:@selector(doAction) forControlEvents:UIControlEventTouchUpInside];

如果您想使用accessoryType,您可以将此代码放入您的cellForRowAtIndexPath 方法中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Pick an indicator to suit your need/look
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

然后你像这样在点击时采取行动

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    // Perform action for this click on row indexPath.row
}

如果您的indexPath.row 不足以区分您的点击(我不明白为什么会这样!),那么您可以随时在您的披露按钮上使用setTag

【讨论】:

  • 将我所拥有的内容更改为 2 列设计,其中右侧列单击将是唯一可以感应到点击的位置会有多困难?
  • 另外,可以提取关于行的哪些信息?喜欢它的订单号吗?或者一些我可以附加的 unique_id?
  • 如果您不想捕获整行的按下,请不要使用didSelectRowAtIndexPath 方法。关于你的第二个问题,我会更新我的答案。
【解决方案2】:
  [self.tableView indexPathForSelectedRow];

还有

  [self.tableView indexPathForCell:sender]; //if you have a sender

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多