Swift 3 - 在UITableViewCell内重新加载UICollectionView

时间:2017-06-08 03:36:12

标签: ios swift uitableview swift3 uicollectionviewcell

我在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
  4. 我在@IBOutlet weak var collectionView: UICollectionView!UITableViewCellUITableViewController拨打到cellForRowAt

    以下是代码:

    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

4 个答案:

答案 0 :(得分:0)

因为您重复使用UITableViewCell所以您必须一直重新加载UICollectionView。如果您使用refreshNow重新加载UICollectionView,则在单元格中刷新否定为false {0},UICollectionView将显示为其重用的单元格=>错误 enter image description here

Udate rep:

请参阅图片,uitableviewcell 1将在索引6处重用。如果不重新加载单元格内容(重新加载集合视图),它将在索引0处显示为uitableviewcell 1

答案 1 :(得分:0)

    #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;
}

答案 2 :(得分:0)

在更新结束时添加:

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

答案 3 :(得分:-1)

在您的情况下,您应该将标记分配给集合视图,以便在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函数之外更新您的数据。