【问题标题】:TableView Section Index not DisplayTableView 部分索引不显示
【发布时间】: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


【解决方案1】:

如果您的本地数据一切正常,我猜您没有遵循优先级。 收到webservice的响应后,必须设置tableview Delegate和Datasource。

self.tableview.dataSource = self
self.tableview.delegate = self

或者你应该重新加载你的tableview:

self.tableview.reloadData()

【讨论】:

  • 当我收到来自 web 服务的响应时,我已经重新加载了 tableview。
  • 请检查您的 numberOfRowsInSection 计数
  • 我的数据显示在表格视图中,但表格视图部分索引不显示
  • 将断点放在返回 sectionIndex 代码行并检查属性数据是否有价值
【解决方案2】:

您是否尝试过这个而不是在部分标题上使用TableViewCell

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18))
    let label = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18))
    label.font = UIFont.systemFontOfSize(14)
    label.text = memberStructList[section].sectionName
    view.addSubview(label)
    view.backgroundColor = UIColor.grayColor() // Set your background color

    return view
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2012-01-09
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2020-09-16
    相关资源
    最近更新 更多