2015-08-28 41 views
0

相當新穎。我試圖將3個自定義單元格填充到TableViewController中。TableViewController中的多個自定義單元格

我已經成功(的幫助),以獲得2毫無問題加載。這是我用於2的代碼:

override func viewDidLoad() { 
super.viewDidLoad() 

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 44 

} 


override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.row % 2 == 0 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell 
     cell.artImageView.image = UIImage(named: "art") 

     return cell 

    } else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell 
     cell.kidsImageView.image = UIImage(named: "kids") 


     return cell 
    } 

當我嘗試添加第三行時,這是我遇到麻煩的地方。這是我使用的代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 44 

} 


override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 3 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.row % 3 == 0 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell 
     cell.artImageView.image = UIImage(named: "art") 

     return cell 

    } else if { 
     let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell 
     cell.kidsImageView.image = UIImage(named: "kids") 


     return cell 

    } else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell 
     cell.pastaImageView.image = UIImage(named: "pasta") 


     return cell 
    } 

} 

任何幫助將是巨大的。格拉西亞斯。

+0

你沒有,如果else語句在第二個指定條件:'}否則,如果{' –

回答

1

你的問題與該行

if indexPath.row % 3 == 0 { 

如果你真的想要的細胞進行佈局1,2,3你可以做到這一點開始:

if indexPath.row == 0 { 
    let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell 
    cell.artImageView.image = UIImage(named: "art") 

    return cell 

} else if indexPath.row == 1 { 
    let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell 
    cell.kidsImageView.image = UIImage(named: "kids") 


    return cell 

} else if indexPath.row == 2 { 
    let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell 
    cell.pastaImageView.image = UIImage(named: "pasta") 


    return cell 
} 

使用indexPath.row % 3 == 0做模數學,所以只有當indexPath.row/3等於一個整數時纔會給你「FirstCell」。然後你有一個else if沒有測試,所以它被調用所有其他行。最後你的else永遠不會被調用。

+0

感謝@Walter,這似乎更多的方式直線前進!雖然我得到一個錯誤,它說:「缺少有望迴歸‘的UITableViewCell’。不知道爲什麼 – CHM

+0

的最後一行需要一個回報函數的回報。所以,你可以重新排列括號或只是把返回的UITableViewCell( )作爲FUNC最後一行。因爲你有上述所有的回報,它就再也不會到最後一行,但是編譯器是不夠聰明,知道這一點。 – Walter

+1

爲最後'其他if'的'別人',並刪除條件。 –