How to write height for row in RxSwift?
Asked Answered
C

3

9

I want to convert the below code into RxSwift. And also please help me how to write button Action code in RxSwift.

ReactiveCocoa or RxSwift Which one is better to use in 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

}
Chesterton answered 19/4, 2017 at 4:46 Comment(0)
M
2

For tableview in RxSwift, take look at : https://github.com/devxoul/help-me-rx-tableview-cell-height/blob/master/SimpleTableViewController/SimpleTableViewController.swift

for the button action check sample code below

button.rx.tap
        .bind { [weak self] in
            //YOUR TAP ACTION CODE LOGIC GOES HERE
        }
        .disposed(by: disposeBag)
Moro answered 6/6, 2017 at 6:48 Comment(0)
A
15

Other answers are outdated. Here's Swift 4/5 version with RxSwift in 2019.

Firstly set up delegate, typically in viewDidLoad:

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

Then of course, write your delegate method(s) somewhere:

extension HostListViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
}
Alton answered 21/4, 2019 at 4:7 Comment(0)
A
7

If you're binding data to a tableView with RxSwift and want to control row/section height, set your UITableViewDelegate like this: tableView.rx.setDelegate(self).disposed(by: disposeBag)

and now your func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat will get called.

Antidisestablishmentarianism answered 9/8, 2018 at 11:42 Comment(0)
M
2

For tableview in RxSwift, take look at : https://github.com/devxoul/help-me-rx-tableview-cell-height/blob/master/SimpleTableViewController/SimpleTableViewController.swift

for the button action check sample code below

button.rx.tap
        .bind { [weak self] in
            //YOUR TAP ACTION CODE LOGIC GOES HERE
        }
        .disposed(by: disposeBag)
Moro answered 6/6, 2017 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.