我有一个应用程序,在单元格的tableview中列出一些数据。我希望用户能够选择任何一个表格视图单元格,让应用程序循环通过5个较低的单元格。以下是我到目前为止的情况:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
[self fetchCellsToProcess:indexPath];
}
这是fetchCellsToProcess:方法:
-(void)fetchCellsToProcess:(NSIndexPath*)indexPath{
for (int cellsToProcess = 0; cellsToProcess < 5; cellsToProcess++) {
//process each cell
//...
}
}
我需要使用indexPath来获取它的indexPath.row。然后将5添加到indexPath.row并仅处理传入的indexPath.row和indexPath.row + 5之间的推文。我应该使用什么编程逻辑来遍历单元格x - &gt; X + 5?
答案 0 :(得分:2)
你不应该在这里使用cellForRowAtIndexPath:
:这是表示逻辑的一部分,而你在这里处理模型级逻辑。
查看您的cellForRowAtIndexPath:
方法,并查看单元格标签文本的来源。通常它是NSArray
或其他一些集合。您的didSelectRowAtIndexPath:
方法应该直接转到同一个集合,并从那里获取信息。
答案 1 :(得分:-1)
没有直接的方法来实现这一点,因为提到了dasblinkenlight
但有一项工作对我有用。
请遵循以下方法:
在自定义单元格中创建自定义按钮
创建自定义单元格类并将按钮(更改类型更改为自定义)执行必要的连接自定义单元类
从您正在实现此逻辑的类中的该按钮创建并连接名为“actionSelectedCell”的操作方法,如下所示。
和cellForRowAtIndexPath cell.customButton.text =数组或字典中的数据
- (IBAction)actionSelectedCell:(UIButton *)sender {
// below code for getting selected cell row and its section
UIView *view = sender.superview;
while (view && ![view isKindOfClass:[UITableViewCell self]]) view = view.superview;
UITableViewCell *cell = (UITableViewCell *)view;
indexPathForSelectedCell = [self.tableViewListViewVC indexPathForCell:cell];
NSLog(@"cell is in section %d, row %d", indexPathForSelectedCell.section, indexPathForSelectedCell.row);// so now you are getting indexpath for selected cell
// now you can create simple loop logic to get required data and do whatever you like post it to something or store it for any other use
NSString *strFifthCellContent = [NSString stringWithFormat:@"%@",[dataArray ObjectAtIndex:indexPathForSelectedCell.row+5]]; // in this way you can get data from any cell
}