Bha*_*ath 8 ios reactive-cocoa rx-swift swift3
我想将以下代码转换为RxSwift. 还请帮助我如何Action在RxSwift.
ReactiveCocoa或者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
}
Run Code Online (Sandbox Code Playgroud)
sup*_*org 10
其他答案已过时。这是 2019 年带有 RxSwift 的 Swift 4/5 版本。
首先设置委托,通常在viewDidLoad:
tableView
.rx.setDelegate(self)
.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
然后当然,在某处编写您的委托方法:
extension HostListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 RxSwift 将数据绑定到 tableView 并想要控制行/节高度,请像这样设置 UITableViewDelegate:
tableView.rx.setDelegate(self).disposed(by: disposeBag)
现在你的func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat遗嘱会被召唤。
对于 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8398 次 |
| 最近记录: |