2011-10-22 53 views
2

当didSelectRowAtIndexPath被调用时,使用以下代码很容易获得单元格的文本值: UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; entryText = cell.textLabel.text;TableView:如何从didSelectRowAtIndexPath例程中选择另一行的值

不过,我在搞清楚如何从另一行同时获得格文本值的问题。例如,如果用户点击0排,上面会得到我从0行的单元格文本,但我需要从第1行和第2行。

单元格文本如何做呢?

回答

0

如果创建了这些细胞自己IndexPath变量,例如:

NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; 
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath1]; 
NSString *cell1Text = [NSString stringWithString: cell1.textLabel.text]; 

NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:indexPath.row+2 inSection:indexPath.section]; 
UITableViewCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath2]; 
NSString *cell2Text = [NSString stringWithString: cell2.textLabel.text]; 

这应该做的伎俩。

+0

完美。谢谢!另一个问题,但。我的表格有多个部分。不是对inSection编号进行硬编码,我该如何提取该值? – user1008723

+0

如果行是在相同的部分使用NSIndexPath发送,从而NSIndexPath * indexPath1 = [NSIndexPath indexPathForRow:1个切入口:indexPath.section]; – Openside

+0

谢谢大家。很棒。最后一个问题是如何提取节标题文本? – user1008723

3

您只需向模型中的数据。你不应该使用视图来存储数据。这对于表格视图单元尤其重要,因为当用户将单元格从屏幕上滚动出来时,视图中的数据可以从一个时刻变为另一个时刻。

0

这真的取决于你如何存储表的数据源。如果它在一个数组中,那么您只需索引数组即可获取该值。

相关问题