ios - 只有在tableview中点击时才会显示数据

标签 ios swift uitableview tableview

我正在开发一个带有表格 View 的应用程序。表格 View 显示大量数据,在几行中,detailTextlabel 仅在我单击它时显示。

这是我用来创建 TableView 的扩展:

extension TicketViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        return TicketsDataSource.sharedInstance.storesPR.count
    case 1:
        return TicketsDataSource.sharedInstance.storesRJ.count
    case 2:
        return TicketsDataSource.sharedInstance.storesSC.count
    case 3:
        return TicketsDataSource.sharedInstance.storesSP.count
    default:
        return 0
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 4
}

func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    return 30
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 30
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

    switch indexPath.section {
    case 0:
        cell.textLabel?.text = TicketsDataSource.sharedInstance.storesPR[indexPath.row]
        cell.detailTextLabel?.text = TicketsDataSource.sharedInstance.addressPR[indexPath.row]
    case 1:
        cell.textLabel?.text = TicketsDataSource.sharedInstance.storesRJ[indexPath.row]
        cell.detailTextLabel?.text = TicketsDataSource.sharedInstance.addressRJ[indexPath.row]
    case 2:
        cell.textLabel?.text = TicketsDataSource.sharedInstance.storesSC[indexPath.row]
        cell.detailTextLabel?.text = TicketsDataSource.sharedInstance.addressesSC[indexPath.row]
    case 3:
        cell.textLabel?.text = TicketsDataSource.sharedInstance.storesSP[indexPath.row]
        cell.detailTextLabel?.text = TicketsDataSource.sharedInstance.addressesSP[indexPath.row]
    default:
        cell.textLabel?.text = ""
        cell.detailTextLabel?.text = ""
    }

    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.textLabel?.font = UIFont.systemFontOfSize(12)

    // While loops to fulfill the tableview
    // Cities of Paraná
    var i = 0
    while i < dataSingleton.citiesPR.count {
        setCell(cell, text: dataSingleton.citiesPR[i])
        i += 1
    }

    // Cities of Rio de Janeiro
    i = 0
    while i < dataSingleton.citiesRJ.count {
        setCell(cell, text: dataSingleton.citiesRJ[i])
        i += 1
    }

    // Fixing bug where SÃO JOÃO DE MERITI was not being displayd differently
    if cell.textLabel?.text == "SÃO JOÃO DE MERITI" {
        cell.textLabel?.font = UIFont.systemFontOfSize(16)
        cell.textLabel?.textColor = UIColor.blackColor()
    }

    // Cities of Santa Catarina
    i = 0
    while i < dataSingleton.citiesSC.count {
        setCell(cell, text: dataSingleton.citiesSC[i])
        i += 1
    }

    // Cities of São Paulo
    i = 0
    while i < dataSingleton.citiesSP.count {
        setCell(cell, text: dataSingleton.citiesSP[i])
        i += 1
    }

    return cell
}

private func setCell(cell: UITableViewCell, text: String) {
    if cell.textLabel?.text == text {
        cell.textLabel?.font = UIFont.systemFontOfSize(16)
        cell.textLabel?.textColor = UIColor.blackColor()
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    var label = ""
    switch section {
    case 0:
        label = TicketsDataSource.sharedInstance.states[0]
    case 1:
        label = TicketsDataSource.sharedInstance.states[1]
    case 2:
        label = TicketsDataSource.sharedInstance.states[2]
    case 3:
        label = TicketsDataSource.sharedInstance.states[3]
    default:
        label = ""
    }

    return label
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel!.font = UIFont.systemFontOfSize(18)
        header.textLabel!.textColor = UIColor.whiteColor()
    }
}

}

As you guys can see, the <code>detailTextLabel</code>, for the middle of the table view on, only displays the data when I click on it

正如你们所看到的,detailTextLabel,在表格 View 的中间,只有在我点击它时才会显示数据。

我不确定问题是否依赖于 cellForRowAtIndexPath 或其他地方。

TickersDataSource 主要是一个数组文件。

有什么想法吗?

提前致谢!

最佳答案

detailTextlabeltextColor 是否可能最初与背景颜色相同,然后在您单击/突出显示该单元格时变为黑色?

关于ios - 只有在tableview中点击时才会显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41769103/

相关文章:

ios - UILabel 中的 HTML 格式 (iOS)

ios - 如何在单个表中使用两个不同的自定义 UITableViewCell 子类?

ios - 删除具有多个TableView的行

iOS:从 SQLite 数据库填充 tableView

ios - 如何创建嵌入框架以便我可以在应用程序扩展和项目中使用它

ios - Collection View 单元格快速滚动到中心时出现问题

ios - Xamarin iOS 应用程序仅在 Release模式下启动时崩溃。 xamarin_initialize EXC_BAD_ACCESS

iOS 流媒体背景音频

swift - 如何监听 WkWebkiew 何时完成加载页面?

swift - UIViewController subview 属性是否应该隐式解包选项?