【问题标题】:Swift 3 - Reload UICollectionView inside the UITableViewCellSwift 3 - 在 UITableViewCell 内重新加载 UICollectionView
【发布时间】:2017-11-09 14:33:48
【问题描述】:

我在 UITableViewCell 中有一个 UICollectionView。您可以参考here的图片

如果发生任何更新,我想重新加载 collectionView。

我做了一些研究,发现了这个:

  1. how to reload a collectionview that is inside a tableviewcell
  2. Reloading collection view inside a table view cell happens after all cells in table view have been loaded
  3. UICollectionView not updating inside UITableViewCell

我在cellForRowAt@IBOutlet weak var collectionView: UICollectionView!UITableViewCell 呼叫到UITableViewController

代码如下:

var refreshNow: Bool = false

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.allCardCell, for: indexPath) as! AllCardTableViewCell

        if refreshNow {
          cell.collectionView.reloadData()
          refreshNow = false
        }

        cell.collectionView.collectionViewLayout.invalidateLayout()
        return cell
    }

如果用户点击 UIAlertAction 上的Ok

let alert = UIAlertController(title: "Success", message: "Card successfully added", preferredStyle: .alert)
                let action = UIAlertAction(title: "Ok", style: .default) { (action) in
                    self.refreshNow = true
                    self.tableView.reloadData()
                }
                alert.addAction(action)
                self.present(alert, animated: true, completion: nil)

我之所以放refreshNow是为了防止应用程序滞后和缓慢。但是如果发生任何变化,仍然没有更新。

问题是collectionView没有刷新。但是我调试的时候,是通过cell.collectionView.reloadData().

更新/更改仅在我重新启动应用程序时发生。我希望它是所谓的real-times 更新。

非常感谢任何帮助,非常感谢。

图片来源:How to use StoryBoard quick build a collectionView inside UITableViewCell

【问题讨论】:

  • 尝试使用调度队列在主线程上调用重载数据。
  • @TusharSharma 哪一部分?在cellForRowAt ?
  • 是的,在那里调用一个调度队列,并在其中调用重新加载数据行。
  • @TusharSharma 还是一样。 if refreshNow { DispatchQueue.main.async { cell.collectionView.reloadData() } refreshNow = false }
  • 是的,尝试一次。

标签: ios swift uitableview swift3 uicollectionviewcell


【解决方案1】:

在更新结束时添加:

DispatchQueue.main.async() { () -> Void in
   self.tableView.reloadData()
}

【讨论】:

  • 在 Swift 4 中工作得很好
【解决方案2】:

在您的情况下,您应该为您的集合视图分配标签,以便在cellForRowAt 函数之外获得访问权限。

你的函数应该是这样的:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.allCardCell, for: indexPath) as! AllCardTableViewCell

    cell.collectionView.tag = 1234
    return cell
}

动作会重新加载,它会使用标签访问collectionView

let action = UIAlertAction(title: "Ok", style: .default) { (action) in
    let collectionView = self.tableView.viewWithTag(1234) as! UICollectionView
    collectionView.reloadData()
}

还要注意cellForRowAt 会在每次单元格出现时根据您在其中添加的内容不断重新加载内容。因此,请在cellForRowAt 函数之外不断更新您的数据。

【讨论】:

  • 我有一个表格视图控制器,其中一个单元格具有集合视图,此代码将在 let collectionView = self.tableView.viewWithTag(1234) 崩溃! UICollectionView
【解决方案3】:

因为您重复使用了UITableViewCell,所以您必须始终重新加载您的UICollectionView。如果您使用 refreshNow 重新加载 UICollectionView,则在单元格处有 refreshNow = false,UICollectionView 将显示为重复使用的单元格 => 错误

Udate 代表:

看,在图片中 uitableviewcell 1 将在索引 6 处重用。如果您不重新加载单元格的内容(重新加载 collectionview),它将在索引 0 处显示为 uitableviewcell 1

【讨论】:

  • 我没找到你。能否请您说的更具体些?
【解决方案4】:
    #import "AddPhotoViewController.h"
    #import "PhotoTableViewCell.h"
    #import "ShareTableViewCell.h"

    @interface AddPhotoViewController ()

    @property (weak, nonatomic) IBOutlet UITableView *tblView;

    @property (strong,nonatomic)NSMutableArray *arrImages,*arrIndexPath,*selectImages;

    @end

    #pragma mark - TableViewDelegate&DataSource

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 3;

    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        UITableViewCell *returnCell;

        static NSString *cellIdentifier = @"CellOne";
        static NSString *cellIdentifierTwo = @"CellTwo";
        static NSString *cellIdentifierThree = @"CellThree";
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            returnCell = cell;
        } else if (indexPath.row == 1){
            ShareTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo forIndexPath:indexPath];
            cell.viewMood.layer.cornerRadius = 5;
            cell.viewPeople.layer.cornerRadius = 5;
            [cell.viewMood layer].borderWidth = 1;
            [cell.viewMood layer].borderColor = [UIColor colorWithRed:241.0/255.0 green:143.0/255.0 blue:48.0/255.0 alpha:1].CGColor;
            [cell.viewPeople layer].borderWidth = 1;
            [cell.viewPeople layer].borderColor = [UIColor colorWithRed:241.0/255.0 green:143.0/255.0 blue:48.0/255.0 alpha:1].CGColor;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            returnCell = cell;
        }else if (indexPath.row == 2){
            PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierThree forIndexPath:indexPath];
            cell.collView.dataSource = self;
            cell.collView.delegate = self;

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            returnCell = cell;
        }

        return  returnCell;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return UITableViewAutomaticDimension;

    }

     #pragma mark-   UIImagePickerControllerDelegate

      -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

        UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
        [_arrImages addObject:chosenImage];

        PhotoTableViewCell *cell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];

        [cell.collView reloadData];

        [picker dismissViewControllerAnimated:YES completion:^{

        }];
    }

#pragma mark - CollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_arrImages count];
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"CellCollection";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    UIImageView *imgView = [(UIImageView*)[cell contentView] viewWithTag:100];
    UIImageView *imgViewTick = [(UIImageView*)[cell contentView] viewWithTag:200];
    UIView *view = [(UIView*)[cell contentView] viewWithTag:300];
    if (indexPath.row == 0){
        imgViewTick.hidden = YES;
        view.hidden = YES;
    }

    if  ([_arrIndexPath containsObject:indexPath]) {

        [_selectImages removeAllObjects];

        view.hidden = NO;
        view.alpha = 0.4;
        imgViewTick.hidden = NO;
        imgView.image = [_arrImages objectAtIndex:indexPath.row];
        [_selectImages addObject:[_arrImages objectAtIndex:indexPath.row]];
        NSLog(@"Pick images:%@",_selectImages);

    }else{
        view.hidden = YES;
        imgViewTick.hidden = YES;

        imgView.image = [_arrImages objectAtIndex:indexPath.row];
    }

    return  cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多