如何在RxSwift中编写行高?

9

我希望你能将以下代码转换为RxSwift。并且请帮助我如何在RxSwift中编写按钮Action的代码。

ReactiveCocoaRxSwift哪个更适合在swift3中使用?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: 
Int) -> Int {
    return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    let cell: BTSTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "BTSTableViewCellIdentifier")! as! BTSTableViewCell

    cell.configureWithPost(posts[indexPath.row])
    cell.delegate = self
    return cell

}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    // 259.5+ labelHeight+ bigImageHeight

    let postViewModel = posts[indexPath.row]

    //caption height
    var captionHeight = postViewModel.textDetail?.height(withConstrainedWidth: tableView.bounds.size.width - 20, font: UIFont.systemFont(ofSize: 17))

    captionHeight = ceil(captionHeight!)

    var finalCaptionHeight: CGFloat = 0.0;

   if postViewModel.showDetailedCaption {
        finalCaptionHeight = captionHeight!
    }
    else {
        finalCaptionHeight = min(41, captionHeight!)
    }
    return 259.5 + 
 postViewModel.getBigImageHeightFor(tableView.bounds.size.width) + 
 finalCaptionHeight

}
3个回答

15

其他答案已经过时。这是2019年使用RxSwift的Swift 4/5版本。

首先在viewDidLoad中设置委托:

tableView
    .rx.setDelegate(self)
    .disposed(by: disposeBag)

当然,接下来在某处编写您的委托方法:

extension HostListViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
}

7
如果您正在使用RxSwift将数据绑定到tableView并希望控制行/节高度,请按照以下方式设置UITableViewDelegate:tableView.rx.setDelegate(self).disposed(by: disposeBag),然后您的func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat将被调用。

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接