簡體   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