Ios UITableview或AQGridView维护单元格中按钮的状态

Ios UITableview或AQGridView维护单元格中按钮的状态,ios,uitableview,aqgridview,Ios,Uitableview,Aqgridview,由于AQGridview借鉴了UITableView的许多思想,因此我认为答案应该适用于两者 我有一个自定义单元格,其中包含以下对象: 标签 收藏夹按钮(可由用户打开/关闭,我使用。选择=是/否) 问题在于滚动时保持按钮的状态。下面是我的“cellForItemAtIndex”方法 - (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) index { stati

由于AQGridview借鉴了UITableView的许多思想,因此我认为答案应该适用于两者

我有一个自定义单元格,其中包含以下对象:

  • 标签
  • 收藏夹按钮(可由用户打开/关闭,我使用。选择=是/否)
问题在于滚动时保持按钮的状态。下面是我的“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;

}
不知何故,我需要在我的viewcontroller中保留一个关于某个项目何时被收藏的引用,以便在使用此方法重新创建单元格时打开/关闭按钮

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


是否有人实现了类似的功能?

正如您所说,将目标按钮添加到您想要的任何方法中。在该方法中,您可以使用

              NSSet *touches = [event allTouches];
              UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tableview];
      NSIndexPath *indexPath = [tableview indexPathForRowAtPoint: currentTouchPosition];
              NSUInteger row = [indexPath row];
然后,您可以在表本身正在使用的现有数据集中存储已按下的按钮。 然后,当加载表并检索数据时,只需根据为该行存储的数据启用/禁用“单元格”按钮:)