【发布时间】:2020-12-25 12:39:21
【问题描述】:
我已经在我的应用中实现了 tableView 部分索引。
TableView Section Index 显示 tableView 的数据何时是本地的,当我从 api 调用获取数据时 tableview 部分索引隐藏。
我不明白为什么会这样
这是我的 tableview 部分索引代码:
var sectionArray = UILocalizedIndexedCollation.current().sectionIndexTitles // section Array
func numberOfSections(in tableView: UITableView) -> Int
{
return memberStructList.count // this is structList
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return memberStructList[section].memberArray.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let cell = tableView.dequeueReusableCell(withIdentifier: "MembersHeaderTVCell") as! MembersTVCell
cell.lblSectionHeader.text = memberStructList[section].sectionName
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "MembersTVCell") as! MembersTVCell
let sectionRows = memberStructList[indexPath.section]
let row = sectionRows.memberArray[indexPath.row]
cell.lblMemberName.text = row.first_name
return cell
}
func sectionIndexTitles(for tableView: UITableView) -> [String]?
{
return sectionArray
}
func tableView(_ tableView: UITableView,
sectionForSectionIndexTitle title: String,
at index: Int) -> Int
{
if memberStructList.contains(where: {$0.sectionName == title}),
let sectionIndex = memberStructList.firstIndex(where: {$0.sectionName == title})
{
return sectionIndex
}
else
{
return NSNotFound
}
}
这是结构代码:
struct MemberStruct
{
var sectionName : String
var memberArray : [MemberModel] = []
}
这是我的 Web 服务代码,MVCServer 是我的 Web 服务函数
MVCServer().serviceRequestWithURL(reqMethod: .get, withUrl: strUrl, withParam: [:], diplayHud: true, includeToken: true) { (ResponseCode, Response) in
if ResponseCode == 1
{
if let array = Response.value(forKeyPath: "payload.data") as? NSArray
{
var memberArray = MemberModel.modelsFromDictionaryArray(array: array)
memberArray.forEach({$0.first_name = $0.first_name.capitalized + " " + $0.last_name.capitalized})
memberArray.sort(){$0.first_name < $1.first_name}
let groupedDictionary = Dictionary(grouping: memberArray, by: {String($0.first_name.capitalized.prefix(1))})
let keys = groupedDictionary.keys.sorted()
self.memberStructList = keys.map({ MemberStruct(sectionName: $0, memberArray: groupedDictionary[$0]!)})
self.tblMembers.reloadData()
}
}
else
{
Utility.showToast(messageData: Response)
}
}
【问题讨论】:
-
在调用 API 并重新加载 tableView 的位置添加代码。
-
@PGDev 我已经更新 API 代码
-
得到响应后检查
self.memberStructList是否为空。 -
@PGDev My TableView 显示我从 api 响应获得的数据,但是,TableViewSection 索引不显示
-
HeaderCell 和表格视图单元格的单元格类是否相同?
标签: ios swift iphone uitableview