gpt4 book ai didi

ios - UITableView只显示部分部分,第三部分不显示

转载 作者:行者123 更新时间:2023-11-30 13:51:00 28 4
gpt4 key购买 nike

 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
}

}

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

enter image description here enter image description here

最佳答案

我认为 MyCellTableViewCell 有问题。也许你可以看看或者包括它?第 3 部分的打印命令是否打印在控制台中?

关于ios - UITableView只显示部分部分,第三部分不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com