首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewController didSelectRowAtIndexPath:(NSIndexPath *)indexPath

UITableViewController didSelectRowAtIndexPath:(NSIndexPath *)indexPath
EN

Stack Overflow用户
提问于 2011-11-27 10:26:34
回答 4查看 4.5K关注 0票数 0

如何引用在didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中单击的单元格对象?

我有一个UISplitViewController,在MasterView中我有一个表,其中的单元格cell.tag =来自sqlite数据库的主键(也就是说,表是由db填充的)。我能够在上面的方法中捕获单击事件,但是我不知道如何传入单元格对象,或者如何引用它来获取cell.tag。最终,目标是通过Master/Detail委托将该id传递给detail视图,然后根据来自Master的id将数据加载到DetailView中。

任何建议都很感谢!

编辑:

代码语言:javascript
复制
- (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] autorelease];
    }

    // Configure the cell.
    cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
    Entry *entry = [self.entries objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[NSString stringWithFormat:@"%@",entry.title]];
    cell.tag = entry.entryID;
    return cell;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-27 11:48:05

因为您已经有了一个条目数组,所以还可以按如下方式编写。

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Entry *entry = [self.entries objectAtIndex:[indexPath row]];
}

我认为这是比cellForRowAtIndexPath更好的方式:因为

  • 您可以获取整个entry对象,而不仅仅是ID。
  • 您可以使用非整数ID,如字符串。
  • 您不依赖于表或单元格(解耦)。
票数 4
EN

Stack Overflow用户

发布于 2011-11-27 11:12:10

didSelectRowAtIndexPath:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}

注意,向表请求cellForRowAtIndexPath:会返回一个单元格,而向控制器请求tableView:cellForRowAtIndexPath:则会运行委托方法。

票数 4
EN

Stack Overflow用户

发布于 2011-11-27 10:31:50

您可以使用cellForRowAtIndexPath方法从NSIndexPath获取UITableViewCell。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8283192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档