如何在 RxSwift 中写入行高?
How to write height for row in RxSwift?
我想将下面的代码转换成 RxSwift
。也请帮助我如何在 RxSwift
.
中编写按钮 Action
代码
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
}
对于 RxSwift 中的表视图,请查看:
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)
如果您使用 RxSwift 将数据绑定到 tableView 并想要控制 row/section 高度,请像这样设置您的 UITableViewDelegate:
tableView.rx.setDelegate(self).disposed(by: disposeBag)
现在您的 func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat
将被呼叫。
其他答案已过时。这是 Swift 2019 年 RxSwift 的 4/5 版本。
首先设置delegate,一般在viewDidLoad
:
tableView
.rx.setDelegate(self)
.disposed(by: disposeBag)
当然,在某处写下你的委托方法:
extension HostListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}
我想将下面的代码转换成 RxSwift
。也请帮助我如何在 RxSwift
.
Action
代码
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
}
对于 RxSwift 中的表视图,请查看: 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)
如果您使用 RxSwift 将数据绑定到 tableView 并想要控制 row/section 高度,请像这样设置您的 UITableViewDelegate:
tableView.rx.setDelegate(self).disposed(by: disposeBag)
现在您的 func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat
将被呼叫。
其他答案已过时。这是 Swift 2019 年 RxSwift 的 4/5 版本。
首先设置delegate,一般在viewDidLoad
:
tableView
.rx.setDelegate(self)
.disposed(by: disposeBag)
当然,在某处写下你的委托方法:
extension HostListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}