簡體   English   中英

動態TableView中的多個單元原型

[英]Multiple cell prototypes in Dynamic TableView

在此處輸入圖片說明

我想創建一個這樣的表,其中一個節中將有兩個單元格。 在第一個單元格中出現配置文件圖像,在第二個單元格中進行描述。 到目前為止,我已經嘗試將Prototypes設置為2,並給每個原型一個唯一的標識符,還為兩個原型創建了兩個類。 但是問題是它顯示了兩行,但是兩行都有相同的數據。

var profileImage = ["angelina","kevin"]
    var userName = ["Angelina Jolie","Vasiliy Pupkin"]
    var requestTitle = ["I have a Wordpress website and I am looking for someone to create a landing page with links and a cart to link up.","second description"]
    var date = ["Feb 03, 16","Feb 03, 16"]

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return profileImage.count

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        // #warning Incomplete implementation, return the number of rows
        return profileImage.count
    }

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



            if(indexPath.row==0){
                let cell = tableView.dequeueReusableCellWithIdentifier("firstCustomCell", forIndexPath: indexPath) as! FirstProductRequestTableViewCell

                cell.userNameLabel.text = userName[indexPath.row]
                cell.profileImage.image = UIImage(named: profileImage[indexPath.row])
                return cell

            }else{




                let cell = tableView.dequeueReusableCellWithIdentifier("secondCustomCell", forIndexPath: indexPath) as! SecondProductRequestTableViewCell

               cell.requestTitleTxtView.text = requestTitle[indexPath.row]
                return cell

                      }
               }

這是您問題的解決方案。

在這種情況下,您需要使用UITableViewSecionHeaderView ,因為我認為在您的方案中,您對配置文件有多個描述,因此請將包含配置文件信息的SecionHeader和包含描述的單元格放進

但是,如果要重復整個單元格,則只需創建一個CustomCell,其中包含配置文件信息和帶有行分隔符的描述。 您可以使用Image或使用高度為1.0且顏色為淺灰色的UIView進行行分隔符。

根據您的描述,返回的節數是正確的,但是對於每個節中的行數,您應該返回2 ,並且在配置單元格時,您應該使用indexPath.section ,您當前正在使用該行選擇數據添加到單元格中(但繼續使用該行選擇要返回的單元格類型)。

因此,該部分用於選擇您要向其顯示詳細信息的人員,而該行用於選擇要顯示的信息:

var profileImage = ["angelina","kevin"]
var userName = ["Angelina Jolie","Vasiliy Pupkin"]
var requestTitle = ["I have a Wordpress website and I am looking for someone to create a landing page with links and a cart to link up.","second description"]
var date = ["Feb 03, 16","Feb 03, 16"]

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return profileImage.count
}

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

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

    if(indexPath.row == 0) {
        let cell = tableView.dequeueReusableCellWithIdentifier("firstCustomCell", forIndexPath: indexPath) as! FirstProductRequestTableViewCell

        cell.userNameLabel.text = userName[indexPath.row]
        cell.profileImage.image = UIImage(named: profileImage[indexPath.section])

        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("secondCustomCell", forIndexPath: indexPath) as! SecondProductRequestTableViewCell
        cell.requestTitleTxtView.text = requestTitle[indexPath.section]

        return cell
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM