繁体   English   中英

UITableView仅显示该部分的一部分,第三部分未显示

[英]UITableView only display part of section,the Third section didn't show

 class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var tableView:UITableView!
var city = ["gz","dl","sz","bj","hz"]
var city2 = ["gd","ln","gx","he","se"]
var city3 = ["a","b","c","d","e"]
let cellid = "reusecell"
let mycellid = "customer"
override func viewDidLoad()
{
    super.viewDidLoad()
    tableView = UITableView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height), style: UITableViewStyle.Grouped)
    tableView.dataSource = self
    tableView.delegate = self
    self.view.addSubview(tableView)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if section == 0
    {
        return city.count
    }else if section == 1
    {
        return city2.count
    }
    else
    {   print("section3 count")
        return city3.count
    }
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if indexPath.section == 0
    {
        var cell = tableView.dequeueReusableCellWithIdentifier(cellid)
        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellid)
        }
        cell?.textLabel?.text = "\(city[indexPath.row])"
        cell?.detailTextLabel?.text = "beautiful place"
        cell?.imageView?.image = UIImage(named: "test.png")
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell?.selectionStyle = UITableViewCellSelectionStyle.Gray
        tableView.beginUpdates()
        return cell!

    }
    else if indexPath.section == 1
    {
        var cell = tableView.dequeueReusableCellWithIdentifier(cellid)
        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellid)
        }
        cell?.textLabel?.text = "\(city2[indexPath.row])"
        cell?.detailTextLabel?.text = "beautiful place"
        cell?.imageView?.image = UIImage(named: "test.png")
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell?.selectionStyle = UITableViewCellSelectionStyle.Gray
        tableView.beginUpdates()
        return cell!
    }
    else
    {
        var mycell:MyCellTableViewCell? = tableView.dequeueReusableCellWithIdentifier(mycellid) as? MyCellTableViewCell
        if mycell == nil
        {
            print("section3")
            mycell = MyCellTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: mycellid)
        }
         print("section3 config")
        mycell?.title?.text = "\(city3[indexPath.row])"
        mycell?.detailTitle?.text = "beautiful place"
        mycell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        mycell?.imageView?.frame = CGRect(x: 0, y: 0, width: 50, height: 100)
        mycell?.imageView?.image = UIImage(named:"test.png")
        mycell?.selectionStyle = UITableViewCellSelectionStyle.Gray
        return mycell!
    }
}

//Section的头部标题
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "城市选择器"
}

//Section的尾部标题
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return "选择相应的城市"
}

//有几个部分
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 3
}

//section尾部的高度
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 28
}

//section头部的高度
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 28
}
//section行的高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    if indexPath.section == 0
    {

        return 60
    }
    else if indexPath.section == 1
    {
        return 60
    }
    else
    {
        return 100
    }
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    tableView.reloadData()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}




class MyCellTableViewCell: UITableViewCell {
var title:UILabel?
var detailTitle:UILabel?
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    title = UILabel(frame: CGRect(x: 100, y: 5, width: 50, height: 40))
    detailTitle = UILabel(frame: CGRect(x: 100, y: 50, width: 50, height: 40))

    self.addSubview(title!)
    self.addSubview(detailTitle!)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func awakeFromNib() {
    super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

自定义单元格,我想让他在第三节中显示,但是当我下拉TableView时,该节的第三段不显示内容。 我不知道出什么问题了。

在此处 输入图像描述在 此处 输入图像描述

我认为MyCellTableViewCell有问题。 也许您可以看一下或包含它? 控制台中是否已打印第3部分的打印命令?

U需要使用以下命令动态更改桌子的高度:

var noOfRows = city.count + city2.count + city3.count;
var currentHeight = self.tableView(tableView, heightForRowAtIndexPath: indexPath)

var finalHeight = noOfRows * Int(currentHeight);

您将需要放置的是正确的委托函数,以确保在渲染表格时更改高度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM