如何引用在didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中单击的单元格对象?
我有一个UISplitViewController,在MasterView中我有一个表,其中的单元格cell.tag =来自sqlite数据库的主键(也就是说,表是由db填充的)。我能够在上面的方法中捕获单击事件,但是我不知道如何传入单元格对象,或者如何引用它来获取cell.tag。最终,目标是通过Master/Detail委托将该id传递给detail视图,然后根据来自Master的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;
}发布于 2011-11-27 11:48:05
因为您已经有了一个条目数组,所以还可以按如下方式编写。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Entry *entry = [self.entries objectAtIndex:[indexPath row]];
}我认为这是比cellForRowAtIndexPath更好的方式:因为
发布于 2011-11-27 11:12:10
在didSelectRowAtIndexPath:中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}注意,向表请求cellForRowAtIndexPath:会返回一个单元格,而向控制器请求tableView:cellForRowAtIndexPath:则会运行委托方法。
发布于 2011-11-27 10:31:50
您可以使用cellForRowAtIndexPath方法从NSIndexPath获取UITableViewCell。
https://stackoverflow.com/questions/8283192
复制相似问题