我想将下面的代码转换为RxSwift
。还请帮我如何写按钮Action
代码在RxSwift
.
ReactiveCocoa
or RxSwift
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
}
其他答案已经过时了。这是 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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)