RxSwift + UITableViewCell如何在heightForRowAt中获取单元格对象

时间:2017-03-25 10:49:48

标签: swift uitableview rx-swift rx-cocoa

我有一个带UITableView的视图控制器。表数据使用RxSwift填充:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import java.util.List;
import java.util.Locale;
import java.util.logging.Level;

import org.apache.commons.logging.LogFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableFooter;
import com.gargoylesoftware.htmlunit.javascript.host.URL;

public class prova {

/**
 * @param args
 * @throws IOException 
 * @throws MalformedURLException 
 * @throws FailingHttpStatusCodeException 
 * @throws InterruptedException 
 */
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {

    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);

    DecimalFormat df = new DecimalFormat("#,##0.00"); // decimali con virgola
    df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ITALY));

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(true);  
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    String serie = "extraliga"; 
    String pagina = "1";
    String nation = "czech-republic";
    String s = "http://www.oddsportal.com/hockey/"+nation+"/"+serie+"/results/#/page/"+pagina+"/";      


    HtmlPage myPage = webClient.getPage(s);     
    final HtmlDivision htmldiv = myPage.getHtmlElementById("tournamentTable");
    final HtmlTable htmltable = (HtmlTable) htmldiv.getElementsByTagName("table").get(0);
    List<HtmlTableDataCell> matches = htmltable.getElementsByAttribute("td", "class", "name table-participant");

    for (int i=0; i < matches.size(); i++) {

        String link =  "http://www.oddsportal.com"+matches.get(i).getElementsByTagName("a").get(0).getAttribute("href");
        HtmlPage page1X2 = webClient.getPage(link);

        // Quote 1X2
        DomNodeList<HtmlElement> quote1X2 = getQuote1X2(page1X2);
        System.out.println(quote1X2.get(1).asText() + "-" + quote1X2.get(2).asText() + "-" + quote1X2.get(3).asText());

        // get click
        HtmlElement tabUO = page1X2.getHtmlElementById("tab-nav-main").getElementsByAttribute("a", "title", "Over/Under").get(0);
        tabUO.click();

        System.out.println(page1X2.<HtmlElement>getByXPath("//a[@class='more']").size());
        for (HtmlElement anchor : page1X2.<HtmlElement>getByXPath("//a[@class='more']")) {
            if (anchor.getOnClickAttribute().contains("P-5.50")) {
                anchor.click();
                HtmlElement parent = (HtmlElement) anchor.getParentNode().getParentNode().getParentNode();
                HtmlTable table = parent.getFirstByXPath(".//table");
                System.out.println(table.asText());
            }
        }

    }

    webClient.close();
}

// FUNCTION

private static DomNodeList<HtmlElement> getQuote1X2(HtmlPage matchPage) {

        HtmlTableBody matchTable = (HtmlTableBody) matchPage.getElementsByTagName("tbody").get(0);
        List<HtmlElement> row = matchTable.getElementsByTagName("tr");

        int j;
        for (j = 0; j<row.size()-1; j++) {
            if (row.get(j).getElementsByTagName("td").get(0).asText().trim().compareTo("bet365.it")==0) {
                break;
            }
        }

        DomNodeList<HtmlElement> quote = null;
        if (j<row.size()-1)
            quote = row.get(j).getElementsByTagName("td");
        else
        {
            HtmlTableFooter matchFootTable = (HtmlTableFooter) matchPage.getElementsByTagName("tfoot").get(0);
            List<HtmlElement> averrow = matchFootTable.getElementsByAttribute("tr", "class", "aver");
            quote = averrow.get(0).getElementsByTagName("td");
        }

    return quote;

}

我有以下委托功能:

let observable = Observable.just(data)
observable.bindTo(tableView.rx.items(cellIdentifier: "CategoryCell", cellType: CategoryCell.self)) { (row, element, cell) in
    cell.setCategory(category: element)
}.disposed(by: disposeBag)

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

我想从func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // cell = nil. let cell = tableView.cellForRow(at: indexPath) let screenWidth = UIScreen.main.bounds.size.width let itemWidth = (screenWidth / 3.0) - 20 let itemHeight = (itemWidth) / 0.75 + 110 return itemHeight } 内部访问单元格对象,但它会给我heightForRowAt。有没有办法在这里访问单元格?我一直在RxCocoa项目中查看UITableView + Rx.swift,但没有这个功能。我还有其他选择吗?

编辑:我正在尝试在我的代码中实现以下功能:

nil

1 个答案:

答案 0 :(得分:2)

调用tableView.cellForRow(at: indexPath)tableView(_: heightForRowAt:)中返回nil,因为在调用cellForRowAt之前,会在特定的indexPath 上调用heightForRowAt。

这里最好的选择是使用自我调整细胞。 (https://www.raywenderlich.com/129059/self-sizing-table-view-cells)。另一个选择是将单元格的大小保持为模型的一部分并在此处访问它们。