如何从IndexPath获取int



我想计算具有行和节的IndexPath的int值,然后使用ObjectAtIndex从NSArray中选择对象。

我只有一个来自UITable的字符串值,所以我试图将所选的indexpath值与实际数组的ObjectAtIndex路径相关联。

这是我到目前为止的代码,但是我得到了一个错误,因为我试图使用indexPath来代替int。

filterDataArray = [dataArrayOfDictionaries objectAtIndex:indexPath];

所以我的问题是我如何改变indexPath成int。

在你的例子中你可以同时使用row和section的值试试这样,

int totalRows = indexPath.row; //current section row value

for (int n = 0; n < indexPath.section; n++) {//here current section value.
    totalRows += [tableView numberOfRowsInSection:n];// here add rows in every section
}

为了更好地理解NSIndexPath,请参阅下面的内容。

如果你使用NSArray和只有一个section的表视图(如果你不指定numberOfSections: method,默认是一个)。试试这个。

filterDataArray = [dataArrayOfDictionaries objectAtIndex:indexPath.row];

最新更新