我想用动态行制作带有动态部分的 Utableview

Posted

技术标签:

【中文标题】我想用动态行制作带有动态部分的 Utableview【英文标题】:I want to made Uitableview with dynamic section with dynamic rows 【发布时间】:2018-09-11 13:37:37 【问题描述】:

我想将部分标题显示为一月、二月、三月(根据数据来自 api),而不是显示一月、三月、二月。

我正在从后端 api 获取这些数据。

wholeDic : NSDictionary = 

January =     (
            
        date = "20-jan-18";
        subtitle = "Only four days remaining";
        title = "School fees of August, 2018School fees of August, 2018";
    ,
            
        date = "21-jan-18";
        subtitle = Holi;
        title = "Holiday on third march";
    
);
february =     (
            
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    ,
            
        date = "20-12-18";
        subtitle = "Holiday issue by Govt. of India";
        title = "Bharat Band";
    
);
march =     (
            
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    ,
            
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    
);

现在我已经获取“key”并添加到数组中

for (key, value) in wholeDic
   sectionTitleArray.add(key)
   print(sectionTitleArray)

当我打印sectionTitleArray 时,控制台显示January, March, February 而不是显示January, February, March

我知道 Dictionary 是一个无序集合,但我想知道如何按顺序获取键?

我的 UitableView 数据源和委托是

func numberOfSections(in tableView: UITableView) -> Int

    return sectionTitleArray.count


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    let sectionTitle : String = sectionTitleArray.object(at: section) as! String
    let sectiondes:NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    return sectiondes.count


This is my tableView .Everything is working fine but I want to display month like JANUARY,FEBRUARY,MARCH as per api data.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! frameNboundTableViewCell
    let sectionTitle : String = sectionTitleArray.object(at: indexPath.section) as! String
    let contentArr : NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    let contentDic : NSDictionary = contentArr.object(at: indexPath.row) as! NSDictionary
    print(contentDic)
    cell.titleLbl.text = contentDic.value(forKey: "title") as? String
    cell.titleLbl.numberOfLines = 0
    return cell


【问题讨论】:

api 响应问题。您不会同时获得相同的密钥序列。而是使用一月:字典使用数组。 那么如果api响应是一月、三月、四月,你会显示二月吗? @George:-不需要显示二月。我想像一月、二月和三月一样按顺序获取所有键。但是当我在数组中添加键时,它显示一月、三月、 2 月。我必须在 uitableview 中将所有键显示为标题(按照 api 数据序列的顺序名称)。请查看我共享的图像。 @SagarBhut : - 我认为 api 响应没有任何问题。我每次都得到相同的数据。请查看我共享的图像并提出一些建议。 如果 api 响应像二月、一月、四月、三月你想如何在 UI 中显示?是二月、一月、四月、三月还是一月、二月、三月、四月? 【参考方案1】:

基于 cmets,您可以将 api 响应更改为数组,并简单地使用索引访问。 json 响应将如下所示

    
  "data" : [
    
      "month" : "January",
      "details" : [
        
          "date" : "20-jan-18",
          "subtitle" : "Only four days remaining",
          "title" : "School fees of August, 2018School fees of August, 2018"
        ,
        
          "date" : "21-jan-18",
          "subtitle" : "Holi",
          "title" : "Holiday on third march"
        
      ]
    ,
    
      "month" : "february",
      "details" : [
        
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        ,
        
          "date" : "20-12-18",
          "subtitle" : "Holiday issue by Govt. of India",
          "title" : "Bharat Band"
        
      ]
    ,
    
      "month" : "march",
      "details" : [
        
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        ,
        
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        
      ]
    
  ]

现在您可以访问 json 数组中的第一个对象,然后检索相应的键并显示它。会是这样的

let object = response.data![index]
object.month // "January"

希望对你有帮助

【讨论】:

是的,它对我很有用。这就是我正在寻找的东西。非常感谢您在这里提供的宝贵时间。抱歉,我的声誉太低,我无法投票给您。这是在 uitableview 中使部分动态化的实际数据模式。希望它对这里的很多人有所帮助。【参考方案2】:

你应该把第一个字母大写:

extension String 
    func capitalizingFirstLetter() -> String 
        return prefix(1).capitalized + dropFirst()
    

    mutating func capitalizeFirstLetter() 
        self = self.capitalizingFirstLetter()
    

然后是章节标题:

let sectionTitleStr = sectionTitleArray.object(at: indexPath.section) as! String
let sectionTitle : String = sectionTitleStr.capitalizingFirstLetter()

【讨论】:

我认为不需要将标题转换为大写字母,因为当我搜索该键的数组时..它会崩溃,因为在 api 响应中没有任何大写字母的键。请参阅下面的代码: 让 contentArr : NSArray = wholeDic.object(forKey: sectionTitle) 为! NSArray 。它会崩溃。

以上是关于我想用动态行制作带有动态部分的 Utableview的主要内容,如果未能解决你的问题,请参考以下文章

如何在颤动中使用listview builder制作动态单选按钮?

我想用 htaccess 使我的长参考链接动态化

我想创建一个带有用户个人资料名称自动输入的联系表格(动态文本)

如何使用 JS 和 CSS 对 HR 标签(水平线)进行动态效果?

如何在 kv 语言中动态制作很多按钮?

如何使用数据库连接制作动态 amchart?