UITableViewSection行指数 [英] UITableViewSection Row Indices

查看:96
本文介绍了UITableViewSection行指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,有三个部分(星期五,星期六,星期日),每个部分都有不同的数据和不同的行数。选中后,所选行将展开,但具有相同索引的其他两个部分中的行也会展开。我只想要单个选定的行进行扩展。

I have a UITableView with three sections (Friday, Saturday, Sunday) each with different data and a different number of rows. When selected, the selected row expands, but so do the rows in the other two sections with the same index. I only want the single selected row to expand.

有没有办法在我的didSelectRowAtIndexPath方法中获取该部分,并在我的heightForRowAtIndexPath方法中使用它,以便将所有其他行保持在未展开状态大小?

Is there a way to also get the the section in my didSelectRowAtIndexPath method, and to use that in my heightForRowAtIndexPath method in order to keep all of the other rows at their unexpanded size?

以下方法都在我的UITableView子类中:

The methods below are both in my UITableView subclass:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
customCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//Close row if tapped a second time
if(self.currentSelection == indexPath.row) {
    self.currentSelection = -1;
}
//Otherwise, expand row
else {
    self.currentSelection = indexPath.row;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight;
if ([indexPath row] == self.currentSelection) {
    rowHeight = 185;
} else rowHeight = 44;
return rowHeight;
}


推荐答案

将索引路径保存为选择而不仅仅是行。例如,当您保存第2行时,每个部分都有第2行,因此该行在所有部分都在扩展。

Save the index path as your selection instead of just the row. At the moment you are saving, say, row 2, and each section has a row 2, so that row is expanding in all sections.

您可以将索引路径比较为如下:

You can compare index paths as follows:

if([indexPath1 isEqual:indexPath2])
    // Do your thing

更好的是, UITableView 有一个 indexPathForSelectedRow 属性,所以你甚至不需要你自己的变量来跟踪这个:

Better yet, UITableView has an indexPathForSelectedRow property, so you don't even need your own variable to keep track of this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight;
if ([indexPath isEqual:tableView.indexPathForSelectedRow]) {
    rowHeight = 185;
} else rowHeight = 44;
return rowHeight;
}

这篇关于UITableViewSection行指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆