UITableView:制作单独的部分并添加标题和副标题
Posted
技术标签:
【中文标题】UITableView:制作单独的部分并添加标题和副标题【英文标题】:UITable View: Making separate sections and adding titles and subtitles 【发布时间】:2017-01-02 12:10:54 【问题描述】:每个标题的单独部分; 在每个部分的标题下应该是副标题,副标题应该是药物。
我能够正确获取numberOfSections
,如何设置正确的部分标题并为该项目调用标题和副标题?
class FinalPageViewController: UITableViewController
var top: [String: AnyObject]!
var subheading: [String: AnyObject]!
var drugs:[String: AnyObject]!
let CellIdentifier = "Cell Identifier"
var sectioHeader = [String] ()
var subheads: [AnyObject]
//Passing the key Top(Array) here and defining it from plist
if let subheads = top["Subhead"] as? [AnyObject]
return subheads
else
return [AnyObject]()
var drug: [AnyObject]
//Passing the key Top(Array) here and defining it from plist
if let drug = top["Drugs"] as? [AnyObject]
return drug
else
return [AnyObject]()
var heads: [AnyObject]
//Passing the key Top(Array) here and defining it from plist
if let heads = top["Head"] as? [AnyObject]
return heads
else
return [AnyObject]()
override func viewDidLoad()
super.viewDidLoad()
//Assigning the key Top here to title
if let name = top["Top"] as? String
title = name
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func numberOfSections(in tableView: UITableView) -> Int
return heads.count
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return subheads.count
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
let titleName = title
if let titleHeader = heads as? [String: AnyObject], let titled = titleHeader["Headings"] as? String
titleName.text = titled
return titleName
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
// Dequeue Resuable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath)
if let head = heads[(indexPath as NSIndexPath).row] as? [String: AnyObject], let title = head["Headings"] as? String
//Topics is the Key inside array top
// Configure Cell
cell.textLabel?.text = title
return cell;
plist 文件有点大,是这样的。
<plist version="1.0">
<array>
<dict>
<key>Chap</key>
<string>ANS</string>
<key>Top</key>
<array>
<dict>
<key>Topics</key>
<string>Cholinergics</string>
<key>Head</key>
<array>
<dict>
<key>Headings</key>
<string>Directly acting</string>
<key>Subhead</key>
<array>
<dict>
<key>Subheadings</key>
<string>Choline esters </string>
<key>Drugs</key>
<array>
<string>Acetylcholine</string>
<string>Bethanechol</string>
<string>Carbhachol</string>
<string>Methacholine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Natural Alkaloids</string>
<key>Drugs</key>
<array>
<string>Nicotine</string>
<string>Pilocarpine</string>
<string>Muscarine</string>
<string>Arecoline</string>
</array>
</dict>
</array>
</dict>
<dict>
<key>Headings</key>
<string>Indirectly acting</string>
<key>Subhead</key>
<array>
<dict>
<key>Subheadings</key>
<string>Reversible Carbamates</string>
<key>Drugs</key>
<array>
<string>Neostigmine</string>
<string>Edrophonium</string>
<string>Physostigmine</string>
<string>Pyridostigmine</string>
<string>Ambenonium</string>
<string>Galantamine</string>
<string>Donepezil</string>
<string>Rivastigmine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Reversible Acridine</string>
<key>Drugs</key>
<array>
<string>Tacrine</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Irreversible Carbamates</string>
<key>Drugs</key>
<array>
<string>Propoxur</string>
</array>
</dict>
<dict>
<key>Subheadings</key>
<string>Irreversible organophosphates</string>
<key>Drugs</key>
<array>
<string>Echothiophate</string>
<string>Parathion</string>
<string>Malathion</string>
<string>Dyflos</string>
<string>Diazinon</string>
<string>Tabun</string>
<string>Sarin</string>
<string>Soman</string>
</array>
</dict>
</array>
</dict>
<dict>
<key>Headings</key>
<string>Cholineesterase Reactivators</string>
<key>Drugs</key>
<array>
<string>Pralidoxime</string>
<string>Obidoxime</string>
<string>Trimedoxime</string>
</array>
</dict>
</array>
</dict>
<dict>
【问题讨论】:
你有heads.count
作为你的numberOfSections
和numberOfRowsInSection
为什么?
哦!!! numberOfRowsInSection 应该是 subheadings.count @Matt
您的代码中存在太多问题。你编译成功了吗?
是的,如果我删除 titleForHeaderInSection 函数...我可以完美运行代码
@Matt 无法获取“标题”键作为 SectionTitle 的字符串
【参考方案1】:
您的变量heads
是一个数组。所以你应该从heads
数组中获取索引为section
的元素,然后从titleHeader
中获取“标题”。
试试这个
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
var titleName = title
if let titleHeader = heads[section] as? [String: AnyObject], let titled = titleHeader["Headings"] as? String
titleName = titled
return titleName
【讨论】:
如何设置每个部分的标题和副标题为副标题和药物?以上是关于UITableView:制作单独的部分并添加标题和副标题的主要内容,如果未能解决你的问题,请参考以下文章
如何在UICollectionView中添加HeaderView,如UITableView的tableHeaderView
在 uitableview 底部创建一个文本字段并发送按钮,例如 Instagram 评论部分