-1

我有一个应用程序,它在表格视图中的单元格中列出了一些数据。我希望用户能够选择一个表格视图单元格,任何一个,并让应用程序循环通过 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 -> x+5?

4

2 回答 2

2

您不应该cellForRowAtIndexPath:在这里使用:这是表示逻辑的一部分,而您正在这里处理模型级逻辑。

看看你的cellForRowAtIndexPath:方法,看看单元格标签的文本是从哪里来的。通常它是一个NSArray或其他一些集合。您的didSelectRowAtIndexPath:方法应该直接转到同一个集合,并从那里获取信息。

于 2013-09-15T02:40:47.333 回答
-1

There is no direct approach to achieve this as dasblinkenlight mentioned
but there is a work around which works for me.
Please follow below approach:
Create your custom button within your custom cell
Create your custom cell class and put button(change type to custom ) do necessary connection custom cell class
create and connect action method named "actionSelectedCell" as below from that button in class you are implementing this logic.
And cellForRowAtIndexPath cell.customButton.text = data from your array or dictionary

- (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




}
于 2013-09-15T12:26:59.340 回答