出列时,不正确的UITableViewCell会被高亮显示

时间:2014-07-17 00:08:55

标签: ios objective-c cocoa-touch uitableview

我编写了一个UITableViewCell的子类,允许水平滑动为用户提供一些操作。这就是我在做的事情:

  1. 创建scrollView
  2. 创建一个buttonView并添加scrollView。
  3. 创建一个UIButton并将所有单元格控件添加为子视图。添加滚动视图。
  4. 将scrollView添加到单元格内容视图。
  5. 对于#3,我设置了突出显示的图像,以便像普通单元格一样感受用户点击。

    问题是当我的桌面视图在iOS 6上加载了6个单元格并且用户点击任何一个单元格时,单元格会正确突出显示并且细节显示正确的单元格。但是如果用户向上滚动并重新使用第一个单元格并且用户点击顶部单元格(第二行),则它旁边的单元格会突出显示。如果用户向上滚动并清除2个单元格并点击顶部单元格,则向下的单元格2单元格将突出显示。虽然抽头单元显示正确单元格的数据。

    有任何线索吗?

    - (id)initWithStyle:(UITableViewCellStyle)iStyle reuseIdentifier:(NSString *)iReuseIdentifier andMenuButtonDetails:(NSArray *)iMenuButtonDetails {
    if ((self = [super initWithStyle:iStyle reuseIdentifier:iReuseIdentifier])) {
        self.catchWidth = kMenuButtonWidth * [iMenuButtonDetails count];
    
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
        self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + self.catchWidth, CGRectGetHeight(self.bounds));
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.scrollEnabled = YES;
        [self.contentView addSubview:self.scrollView];
    
        self.scrollViewButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - self.catchWidth, kScreenOrigin, self.catchWidth, CGRectGetHeight(self.bounds))];
        [self.scrollView addSubview:self.scrollViewButtonView];
    
        if ([iMenuButtonDetails count]) {
            // Adding menu buttons to the cell.
            CGFloat anXOffset = kScreenOrigin;
            for (NSDictionary *aMenuButton in iMenuButtonDetails) {
                if ([aMenuButton containsObjectForKey:kTitleKey]) {
                    UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(anXOffset, kScreenOrigin, kMenuButtonWidth, kCellHeight64)];
                    [aButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
                    if ([aMenuButton containsObjectForKey:kButtonTagKey])
                        aButton.tag = [[aMenuButton stringForKey:kButtonTagKey] intValue];
                    aButton.titleEdgeInsets = UIEdgeInsetsMake(kScreenOrigin, 2.0, kScreenOrigin, 2.0);
                    aButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
                    [aButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
                    [aButton setTitle:[aMenuButton stringForKey:kTitleKey] forState:UIControlStateNormal];
                    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
                    if ([aMenuButton objectForKey:kButtonColorKey]) {
                        aButton.backgroundColor = [aMenuButton objectForKey:kButtonColorKey];
                    }
                    [self.scrollViewButtonView addSubview:aButton];
                    anXOffset += kMenuButtonWidth;
                }
            }
        }
    
        self.scrollViewContentView = [UIButton buttonWithType:UIButtonTypeCustom];
        self.scrollViewContentView.frame = CGRectMake(kScreenOrigin, kScreenOrigin, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
    
        if (![Utilities isIOS7orAbove]) {
            [self.scrollViewContentView addTarget:self action:@selector(cellHighlighted) forControlEvents:UIControlEventTouchDown];
            [self.scrollViewContentView addTarget:self action:@selector(cellCancelHighlight) forControlEvents:UIControlEventTouchDragExit];
        }
        [self.scrollViewContentView addTarget:self action:@selector(selectCell:) forControlEvents:UIControlEventTouchUpInside];
        self.scrollViewContentView.backgroundColor = [UIColor whiteColor];
    
        UIImage *aBGHighlightedImage = nil;
    
        if ([Utilities isIOS7orAbove]) {
            aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS7];
        } else {
            aBGHighlightedImage = [UIImage imageNamed:kCellHighlightedImageIOS6];
        }
    
        [self.scrollViewContentView setBackgroundImage:[aBGHighlightedImage stretchableImageWithLeftCapWidth:11.0f topCapHeight:0] forState:UIControlStateHighlighted];
        [self.scrollView addSubview:self.scrollViewContentView];
    
    
        [self.scrollViewContentView addSubview:self.imageView];
        [self.scrollViewContentView addSubview:self.textLabel];
        [self.scrollViewContentView addSubview:self.detailTextLabel];
    }
    
    - (void)prepareForReuse {
        [super prepareForReuse];
        self.scrollViewContentView.enabled = YES;
        [self.scrollView setContentOffset:CGPointZero animated:NO];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
        MyTableViewCell *aCell = (MyTableViewCell *)[iTableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    
        if (!aCell) {
            aCell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier" andMenuButtonDetails:aMenuButtons];
        }
    
        // Set data on cell now
    
        return aCell
    }
    

1 个答案:

答案 0 :(得分:0)

如果我在这里遗失了一些东西,请告诉我,但您似乎无缘无故地为您的班级增加了大量的复杂性。你熟悉UICollectionView吗?

这是一个示例实现(水平滚动):

@interface asdf () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@end

@implementation asdf

- (id)init
{
    self = [super init];
    if (self)
    {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.collectionViewLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.view addSubview:self.collectionView];

        NSString *className = NSStringFromClass([UICollectionViewCell class]);
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:className];
    }

    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.collectionView.frame = self.view.bounds;
}

- (UICollectionViewLayout *)collectionViewLayout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return layout;
}

#pragma mark - UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *className = NSStringFromClass([UICollectionViewCell class]);
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:className forIndexPath:indexPath];

    // This is for dequeuing
    NSInteger tag = 12324;
    UIView *view = [cell viewWithTag:tag];
    if (view)
        [view removeFromSuperview];

    view = [[UIView alloc] initWithFrame:cell.bounds];
    view.tag = tag;

    // Add all of your subviews to the view property

    [cell addSubview:view];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.bounds.size.width, 50);
}

@end

我作为样本快速写了这个,所以它不是专门针对你正在构建的内容而定制的,但这应该让你很清楚实现UICollectionView是多么简单。

这个答案对于你所要求的内容可能是随机的,但是在可能的情况下,你应该总是尝试使用Apple所提供的内容而不是花费宝贵的时间重新发明轮子w /&amp;可能会遇到像你这样的随机细微差别。