如何在RxSwift中写入行的高度?

问题描述 投票:4回答:3

我想将以下代码转换为RxSwift。还请帮助我如何在Action中编写按钮RxSwift代码。

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

}
ios swift3 reactive-cocoa rx-swift
3个回答
2
投票

有关RxSwift中的tableview,请查看:https://github.com/devxoul/help-me-rx-tableview-cell-height/blob/master/SimpleTableViewController/SimpleTableViewController.swift

对于按钮操作检查下面的示例代码

button.rx.tap
        .bind { [weak self] in
            //YOUR TAP ACTION CODE LOGIC GOES HERE
        }
        .disposed(by: disposeBag)

4
投票

如果您使用RxSwift将数据绑定到tableView并想要控制行/节高度,请将UITableViewDelegate设置为:tableView.rx.setDelegate(self).disposed(by: disposeBag)

现在你的func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat将被召唤。


0
投票

其他答案已过时。这是在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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.