简体   繁体   中英

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
}

}

Custom Cell, I want to let him in the third section shows, but when I pulled down the TableView, the third paragraph of section does not display the content. I don't know what's the problem.

enter image description here enter image description here

I assume that there is something wrong the MyCellTableViewCell. Maybe you can have a look at that or include it? Are the print commands from part#3 printed in the console?

U need to dynamically change the height of the table using the following:

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

var finalHeight = noOfRows * Int(currentHeight);

You will need to place is the correct delegate function to make sure that when the table is rendered the height is changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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