将进度设置为UITableViewCell元素

时间:2017-04-27 10:24:30

标签: ios swift uitableview rx-swift

我有一个带有进度条的自定义UIView的UITableViewCell。通过单击单元格应用程序开始从Alamofire加载API中的一些内容。我知道如何获取当前下载的进度以及如何将其传递给进度视图,但我正在使用RxSwift + Realm,当我滚动UITableView单元格的进度未显示时。我想在即时滚动UITableView时显示进度。我试图在单元格的Realm元素变量中存储进度并在单元格中订阅此变量,但此操作会加载MainTread。

这是我的UITableView的数据源

let tableData: Observable<[Song]> = Observable.combineLatest(loadedData.asObservable(), dbData.asObservable()) { (loadedData, dbData) -> [Song] in
        var newArray = self.currentPlaylist == nil ? loadedData.filter({ !$0.isInvalidated }) : dbData.filter({ !$0.isInvalidated && self.currentPlaylist!.plSongs.contains($0) })

        if self.currentPlaylist == nil {
            _ = dbData.filter({ !$0.isInvalidated }).map({ data in
                if newArray.contains(where: { $0.compoundKey == data.compoundKey && !data.isInvalidated }) {
                    newArray[newArray.index(where: {$0.compoundKey == data.compoundKey && !data.isInvalidated })!] = data
                }
            })
        }
        return newArray.filter({ !$0.isInvalidated })
    }

    tableData
        .bind(to: self.tableView.rx.items(cellIdentifier: "SongCell", cellType: SongTableViewCell.self)) { (_, element, cell) in
            cell.songResponse = element

            cell.selectionStyle = .none
            cell.backgroundColor = .clear
        }
        .disposed(by: bag)

这是UITableView Cell

var songLoadedProgress = Variable<Float?>(nil)
var songResponse: Song? {
    didSet {
        self.layoutCell()
    }
}

func layoutCell() {
    songLoadedProgress.value = songResponse?.songLoadedProgress

            songLoadedProgress.asObservable()
        .bind { (progress) in
            guard let loadingProgress = progress else { return }
            self.downloadView.circleView.setProgress(value: CGFloat(loadingProgress), animationDuration: 0.1)
        }
        .disposed(by: cellBag)
}

感谢!!!

0 个答案:

没有答案