2011-11-27 66 views
0

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

我有一个UISplitViewController,在MasterView中我有一个表,其中cell.tag = sqlite数据库的主键(即从db填充表)。我能够捕获上述方法中的点击事件,但我看不到我如何传递单元格对象,或者我可以如何引用它以获取cell.tag。最终,目标是通过主/细节委托将该ID传递给详细视图,然后根据来自主人的ID将数据加载到DetailView中。

任何提示,感激!

编辑:

- (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; 
} 

回答

4

因为您已经拥有一组条目,您还可以按如下方式书写。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    Entry *entry = [self.entries objectAtIndex:[indexPath row]]; 
} 

我觉得这是比的cellForRowAtIndexPath首选方式:因为

  • 你可以得到整个条目对象,不仅ID。
  • 你可以使用非整数ID像字符串。
  • 你不依赖于表或单元格(解耦)。
+0

是的,这更好。我认为我必须通过我再次查询数据库,但如果我可以通过整个入口对象,这是最好的情况。谢谢! – David

2

您可以使用该方法cellForRowAtIndexPath从NSIndexPath得到的UITableViewCell。

+0

这实际上是我填充我感兴趣的单元格属性。请参阅上面的编辑。你是说我可以从didSelectRowAtIndexPath方法中调用该方法吗? – David

0

您可以通过它保存所有ID中的NSMutableArray,然后用这种方法在其他类传递..

classInstanceName.IntVariableName=[[taskIdArray objectAtIndex:indexPath.row]intValue]

4

didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
} 

注意如何询问表cellForRowAtIndexPath:返回一个单元格,而要求控制器tableView:cellForRowAtIndexPath:运行委托方法。

+0

这是我需要的。谢谢! – David

相关问题