2

调用 didSelectRowAtIndexPath 时,很容易使用以下方法获取单元格的文本值: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; entryText = cell.textLabel.text;

但是,我在弄清楚如何同时从另一行获取单元格文本值时遇到了问题。例如,如果用户点击第 0 行,上面的代码会从第 0 行获取单元格文本。但我需要从第 1 行和第 2 行获取单元格文本。

我怎么做?

4

3 回答 3

3

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

于 2011-10-22T16:50:14.790 回答
0

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

于 2011-10-22T16:51:13.860 回答
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];

这应该够了吧。

于 2011-10-22T16:56:01.367 回答