3

我有 2 个视图,UITableViewCell我想在每个单元格中显示备用视图(如果前一个单元格的右视图打开,那么下一个单元格的左视图将打开)。

我已经成功实现了它,但是在滚动表格视图时,交替视图无法正常工作;下面的示例图片:

问题图片

我的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveStreamCell" forIndexPath:indexPath];
    
    if(cell == nil)
    {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LiveStreamCell"];
    }

    
    UIView *leftView = (UIView*)[cell viewWithTag:999];
    UIView *rightView = (UIView*)[cell viewWithTag:998];

    UIImageView *mediaImage;
    mediaImage.clipsToBounds = YES;
    
    UILabel *artistLabel;
    UILabel *titleLabel;
    UILabel *statusLabel;
    
    if (isViewOnLeft) {
    
        isViewOnLeft = NO;
        
        rightView.hidden = NO;
        leftView.hidden = YES;
        
        mediaImage = (UIImageView *)[cell viewWithTag:221];
        
        CALayer *cellImageLayer = mediaImage.layer;
        [cellImageLayer setCornerRadius:74];
        [cellImageLayer setMasksToBounds:YES];
        
        artistLabel = (UILabel *)[cell.contentView viewWithTag:222];
        titleLabel = (UILabel *)[cell.contentView viewWithTag:223];
        statusLabel = (UILabel *)[cell.contentView viewWithTag:224];
        
        
    }
    else {
        
        isViewOnLeft = YES;
        
        leftView.hidden = NO;
        rightView.hidden = YES;
        
        mediaImage = (UIImageView *)[cell viewWithTag:121];
        
        CALayer *cellImageLayer = mediaImage.layer;
        [cellImageLayer setCornerRadius:74];
        [cellImageLayer setMasksToBounds:YES];
        
        artistLabel = (UILabel *)[cell.contentView viewWithTag:122];
        titleLabel = (UILabel *)[cell.contentView viewWithTag:123];
        statusLabel = (UILabel *)[cell.contentView viewWithTag:124];  
    }

   /*Here I'm setting image (come from web URLs) and labels data*/
}

故事板截图:

故事板

我知道问题在于细胞的可重用性,但我搜索了很多但没有运气。请提出解决方案。

谢谢!

4

1 回答 1

0

相反,您可以创建两个表格视图单元格,并基于 indexPath,如果它的奇数或偶数加载左侧或右侧单元格。假设你有奇怪的加载左图像视图。

这样,您只需更改获取单元格的代码。其余部分保持不变。

于 2016-01-17T09:15:25.720 回答