繁体   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