没有调用TableView的cellForRowAtIndexPath?

时间:2015-11-03 18:21:48

标签: ios swift uitableview

我正在尝试在Swift中构建一个简单的TableView,但是我想创建一个将用作DataSource的新类,而不是符合我的ViewController中的数据源协议。不幸的是,通过这种方法,我无法在ViewController中加载任何内容。

这是我的ViewController类:

class SaladViewController: UIViewController {

    @IBOutlet weak var saladTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let tableData = LunchTableData()
        self.saladTable.dataSource = tableData
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这是我的DataSource类:

class LunchTableData: NSObject, UITableViewDataSource {

    var things = ["One", "Two", "Three"]

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return things.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")

        let itemText = cell!.viewWithTag(100) as! UILabel
        itemText.text = things[indexPath.row]

        return cell!
    }
}

RowAtIndexPath被称为正常,所以我不确定为什么没有调用CellForRowAtIndexPath。我设置了一个断点,它永远不会打到它。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

我认为viewDidLoad()完成后会破坏tableData。

所以在@IBOutlet弱var saladTable下面移动以下行:UITableView!线。

public class Pilot
{
private String name;
private String license;

/** Parameterized constructor for Pilot.
 * 
 * Sets the fields using the parameter values.
 */
public Pilot(String name, String license)
{
    this.name = name;
    this.license = license;
}  

/** No-arg Constructor for Pilot.
 * 
 * Sets the fields name and license to blank instead of them being null. 
 * This prevents the program from crashing if another method is called 
 * before the name and license objects' fields are made to reference 
 * the String objects.
 */
public Pilot()
{
    name = "";
    license = "";
}    

//I have clue if this getLength() is needed. I was just trying 
//code from my textbook
public int getLength()
{
    int len = 0;

    if (name != null)
        len += name.length();

    if (license != null)
        len += license.length();   

    return len;

}    

/** Mutator for name.
 * 
 * @param {String} name - Somebody's name.
 */
public void setName(String name)
{
    this.name = name;
}

/** Mutator for license.
 * 
 * @param {String} license - Somebody's license.
 */
public void setLicense(String license)
{
    this.license = license;
}

/** Accessor for name.
 * 
 * @return Returns name
 */
public String getName()
{
    return name;
}

/** Accessor for license.
 * 
 * @return Returns license
 */
public String getLicense()
{
    return license;
}    

/** copy() method for Pilot class.
 * 
 * @return Returns p A new object that is an object of Pilot 
 * @param {Object[]} - copy method of Pilot
 */    
public Pilot copy()
{
    // Create a new Pilot object and initialize it
    // with the same data held by the calling object.
    Pilot pilot = new Pilot(name, license);

    // Return a reference to the new object.
    return pilot; 

    // book code, may need as reference
    // Pilot copyObject = new Pilot(name, license);
    // return copyObject;        
}

/**
 * toString() method for Pilot class.
 * 
 * @return Returns String.format which determines the print format of the 
 * pilot later in the class.
 * @param {String} - toString() method 
 */    
public String toString()
{
    return String.format("%s %1s %1s %1s %1s", 
                         "Name:",name, "-", 
                         "License:", license);
}    

/** 
 * equals() method for Pilot class.
 * 
 * @return Returns status
 * @param {Object[]} pilot 
 */    
public boolean equals(Pilot pilot)
{   
    boolean status;
    // Determine whether this object(pilot) name and
    // license fields are equal to Pilot's 
    // name and license fields.
    if (this.name.equals(pilot.name) &&
        this.license.equals(pilot.license))
        status = true;  // Yes, the objects are equal.
    else
        status = false; // No, the objects are not equal.

    // Return the value in status.
    return status;
}

/**
 * The main method.
 * 
 * @param {args} Runs the main method. A new Pilot object is created(pilot) 
 * with values for the name and license Strings. The new Pilot object(pilot)
 * is then printed as "Name: Tom - License: 12345-2" based off the 
 * formatting in the toString() method.
 */    
public static void main(String[] args)
{
    Pilot pilot = new Pilot("Tom", "12345-2");  
    System.out.println(pilot);
}

希望这能帮到你

如此完整的代码

let tableData = LunchTableData()