TableView 仅在单击时显示内容
Posted
技术标签:
【中文标题】TableView 仅在单击时显示内容【英文标题】:TableView only shows content when clicked 【发布时间】:2016-06-04 18:37:24 【问题描述】:我正在制作一个 TableView 来显示我们镇上的商店以及它们的类别。我在表格视图中显示商店时遇到问题:首先,第一家商店没有显示,其次,商店只有在点击时才可见。你可以找到问题的视频here
表格视图的代码:
import UIKit
class MasterTableViewController: UITableViewController
// MARK: - Properties
var detailViewController: DetailViewController? = nil
var winkels = [Winkel]()
// MARK: - View Setup
override func viewDidLoad()
super.viewDidLoad()
winkels = [
Winkel(category:"Literature", name:"Standaard"),
Winkel(category:"Literature", name:"Acco"),
Winkel(category:"Clothing", name:"H&M"),
Winkel(category:"Clothing", name:"C&A"),
Winkel(category:"Clothing", name:"Patio"),
Winkel(category:"Restaurants", name:"De 46"),
Winkel(category:"Restaurants", name:"Het hoekske"),
Winkel(category:"Supermarkets", name:"Carrefour"),
Winkel(category:"Supermarkets", name:"Colruyt")
]
if let splitViewController = splitViewController
let controllers = splitViewController.viewControllers
detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? DetailViewController
override func viewWillAppear(animated: Bool)
clearsSelectionOnViewWillAppear = splitViewController!.collapsed
super.viewWillAppear(animated)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
return 1
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return winkels.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let winkel = winkels[indexPath.row]
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
cell.textLabel!.text = winkel.name
cell.detailTextLabel?.text = winkel.category
return cell
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "showDetail"
if let indexPath = tableView.indexPathForSelectedRow
let winkel = winkels[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailWinkel = winkel
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
有人知道如何解决这些问题吗?提前致谢!
【问题讨论】:
【参考方案1】:删除行cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
因为你已经有了var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
【讨论】:
如果我这样做,它确实会立即显示所有名称,但不会显示类别,当我单击它时也不会显示。我可以将两者结合起来吗? 是的,只需将您的单元格原型样式更改为情节提要中的字幕即可。【参考方案2】:在 viewDidLoad
函数的末尾添加这一行
self.tableView.reloadData()
【讨论】:
嗯,它似乎没有做任何事情:/以上是关于TableView 仅在单击时显示内容的主要内容,如果未能解决你的问题,请参考以下文章
如何默认隐藏键盘并仅在单击 EditText 时显示 [重复]
隐藏一个 subTabBar,它应该在 tableView 中单击标题时显示,用于 iPad 应用程序
Android - 仅在 AsyncTask 未完成时单击按钮时显示进度对话框