gpt4 book ai didi

ios - 加载后 Swift UITableView 消失

转载 作者:搜寻专家 更新时间:2023-11-01 07:27:05 24 4
gpt4 key购买 nike

我目前遇到一个问题,当我通过 IB 创建 UITableView 并显示它时。 UITableView 一开始加载得很好,它显示了我的 4 个单元格,然后立即消失了。

我相信 tableview 只是消失了,因为容器 View 背景可以更改并且仍然存在。

在点击嵌入到 UINavigationBar 中的 UISearchBar 之后,我正在从 Storyboard 中加载 UITableView。然后我将它插入容器中。然后,容器会与 UITableView 一起显示在屏幕上。

调用如下:

var searchFestivalTableViewContainer: UIView?

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
showSearchView()
}

func showSearchView() {

searchBar.showsCancelButton = true

let storyboard = UIStoryboard(name: "Main", bundle: nil)

// Setup the festival search controller inside the container

searchFestivalTableViewContainer = UIView(frame: CGRectMake(0, 64, UIScreen.mainScreen().bounds.width, (UIScreen.mainScreen().bounds.height-64)))
searchFestivalTableViewContainer!.backgroundColor = UIColor.clearColor()


let searchFestivalTableController = storyboard.instantiateViewControllerWithIdentifier("searchFestivalTableViewController") as? searchFestivalTableViewController

searchFestivalTableController!.festivalList = allFestivals!

searchFestivalTableViewContainer!.addSubview(searchFestivalTableController!.view)

self.view.addSubview(searchFestivalTableViewContainer!)

}

类的代码是:

class searchFestivalTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var festivalList: [Festival]?

struct Objects {
var sectionName : String!
var sectionObjects : [Festival]!
}

var allFestivals = [Objects]()

@IBOutlet weak var tableView: UITableView!

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidLoad() {
super.viewDidLoad()

allFestivals = getFestivals()

tableView.reloadData()

}

func getFestivals() -> Array<Objects> {

var festivalSearchResult = [String: Array<Festival>]()

festivalSearchResult["Results"] = [Festival(// Hiding data structure)]

var objectArray = [Objects]()

for festival in festivalList! {

festivalSearchResult["Results"]!.append(festival)

}

let sortedFestivals = festivalSearchResult.sort { $0.0 < $1.0 }

for (key, value) in sortedFestivals {
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}


return objectArray
}



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return allFestivals.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allFestivals[section].sectionObjects.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

let sectionHeader = allFestivals[section].sectionName

return sectionHeader

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let festivalInfo = allFestivals[indexPath.section].sectionObjects[indexPath.row]

let cell = tableView.dequeueReusableCellWithIdentifier("searchFestivalTableCell", forIndexPath: indexPath) as! searchFestivalTableViewCell

cell.textLabel?.text = festivalInfo.festivalName

return cell


}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

// Nothing to do here yet

}

}

class searchFestivalTableViewCell: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()
}

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

这是 tableview 闪烁后的当前输出

Link to image

请问您能帮忙解释为什么 tableview 会运行吗?控制台中没有任何错误或信息。

谢谢!

最佳答案

我认为您应该将 searchFestivalTableViewController 添加为 childViewController到你的第一个view controller , 在添加其 view 之前作为 subview .虽然不确定,但这是我尝试完成它的方式。更多信息在这里: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

关于ios - 加载后 Swift UITableView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35301755/

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