繁体   English   中英

Swift 3-在UITableViewCell内部重新加载UICollectionView

[英]Swift 3 - Reload UICollectionView inside the UITableViewCell

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

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

我做了一些研究发现:

  1. 如何重新加载tableviewcell内的collectionview
  2. 加载表格视图中的所有单元格后,会在表格视图单元格中重新加载集合视图
  3. UICollectionView不在UITableViewCell内部更新

我把@IBOutlet weak var collectionView: UICollectionView!称为@IBOutlet weak var collectionView: UICollectionView! UITableViewCellcellForRowAt 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更新。

非常感谢您的帮助,非常感谢。

图片来源: 如何使用StoryBoard在UITableViewCell内部快速构建collectionView

因为您重用了UITableViewCell所以必须UICollectionView重新加载UICollectionView 如果您使用refreshNow重新加载UICollectionView ,则在单元格具有refreshNow = false的情况下, UICollectionView会像重用的单元格一样显示=>错误 在此处输入图片说明

Udate代表:

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

在更新结束时添加:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM