0

由于 AQGridview 从 UITableView 借用了很多它的想法,所以我认为答案应该适用于两者。

我有一个自定义单元格,里面有以下对象:

  • 标签
  • 最喜欢的按钮(可以由用户打开/关闭,我使用 .selected = YES/NO)

问题是滚动时保持按钮的状态。下面是我的“cellForItemAtIndex”方法。

- (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * cellIdentifier = @"CellIdentifier";

    SampleGridViewCell * cell = (SampleGridViewCell *)[aGridView dequeueReusableCellWithIdentifier:cellIdentifier];

    if ( cell == nil )
    {
        cell = [[SampleGridViewCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 200, 60)                                   reuseIdentifier: cellIdentifier];
    }

    NSDictionary *item = [self.items objectAtIndex:index];

    NSString *title = [item objectForKey:@"title"];

    cell.index = index;

    cell.titleLabel.text = title;

    //cell.favButton.selected = (logic goes here);

    return cell;

}

不知何故,我需要在我的视图控制器中保留一个项目何时被收藏的参考,以便我可以在此方法中重新创建单元格时打开/关闭按钮。

我是否使用 vc 中的方法在 cell.favButton 上执行 addTarget?但是,我如何获得对按钮索引的引用呢?

有人实现了类似的东西吗?

4

1 回答 1

0

正如您所说,为您添加一个目标按钮到您想要的任何方法。在该方法中,您可以使用以下方法获取行索引

              NSSet *touches = [event allTouches];
              UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tableview];
      NSIndexPath *indexPath = [tableview indexPathForRowAtPoint: currentTouchPosition];
              NSUInteger row = [indexPath row];

然后,您可以从那里将已按下的按钮存储在表本身正在使用的现有数据集中。然后,当表格被加载并检索数据时,只需根据您为该行存储的数据启用/禁用单元格按钮 :)

于 2012-08-20T09:13:39.110 回答