如何在iOS中将sqlite中提取的数据显示到tableview上

时间:2016-05-16 10:55:45

标签: ios arrays json sqlite uitableview

我的sqlite数据库中保存了json数据。我从数据库中获取了数据(moodsDataArray),看起来像这样 -

{     

data = "(\n       
{\n        c1 = 8847FF;\n        c2 =    EA8CFF;\n        c3 = D1FFB0;\n        c4 = FF63FC;\n        c5 = 6B6B6B;\n        description = mood;\n        \"font_color\" = 000000;\n        \"font_name\" = Default;\n        \"font_size\" = 3;\n        \"moods_name\" = mood1;\n    },\n        
{\n        c1 = D4FF38;\n        c2 = FFA83D;\n        c3 = FFFA9E;\n        c4 = 66FFBA;\n        c5 = 63FFE8;\n        description = \"this is mood 2\";\n        \"font_color\" = 363636;\n        \"font_name\" = Default;\n        \"font_size\" = 10;\n        \"moods_name\" = mood2;\n    },\n      
{\n        c1 = 52FFA5;\n        c2 = B8F2FF;\n        c3 = FF6EA5;\n        c4 = DFC4FF;\n        c5 = 61FFED;\n        description = \"it is mood 3\";\n        \"font_color\" = 595959;\n        \"font_name\" = Default;\n        \"font_size\" = 4;\n        \"moods_name\" = mood3;\n    },\n       
{\n        c1 = 8791FF;\n        c2 = D8FF63;\n        c3 = 3DFFDB;\n        c4 = C0FF5C;\n        c5 = FF9EE7;\n        description = \"this is mood 4\";\n        \"font_color\" = 242424;\n        \"font_name\" = Serif;\n        \"font_size\" = 4;\n        \"moods_name\" = mood4;\n    },\n    
{\n        c1 = 8AFFFD;\n        c2 = B3FFC3;\n        c3 = DB70FF;\n        c4 = AEFF9C;\n        c5 = 70FDFF;\n        description = \"this is mood 5\";\n        \"font_color\" = 2134FF;\n        \"font_name\" = Monospace;\n        \"font_size\" = 4;\n        \"moods_name\" = mood5;\n    },\n        
{\n        c1 = 6BFF69;\n        c2 = FF575F;\n        c3 = 78FDFF;\n        c4 = 61FF36;\n        c5 = 6D1FFF;\n        description = \"6th mood\";\n        \"font_color\" = 212121;\n        \"font_name\" = Serif;\n        \"font_size\" = 11;\n        \"moods_name\" = mood6;\n    },\n    
{\n        c1 = FFA72B;\n        c2 = FFD8A1;\n        c3 = FFE38F;\n        c4 = FAFFC7;\n        c5 = FFFBC9;\n        description = \"it is mood 9\";\n        \"font_color\" = C66BFF;\n        \"font_name\" = Sans;\n        \"font_size\" = 11;\n        \"moods_name\" = mood9;\n    }\n)";
    message = "moods Details!";
    status = 1;
}

现在我创建了一个表格视图,我必须将标题名称设置为' moods_name'这是上述数组中的一个关键。我在cellForRowAtIndexPath中尝试了以下方法,但它对我不起作用。

cell.lblmoodstitle.text=[[_moodsDataArray objectAtIndex:indexPath.row]valueForKey:@"moods_name"];

请帮助我,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

以上发布的结果是字典格式。有3个键:数据,消息和状态。并且每个键的值都以String形式返回。因此,您无法将其作为数组访问。

更新: 对于每个数组元素,您需要包含来自"{" to "}"的子字符串的字典。因此,首先在"\n" by ""(empty space)".之后替换所有replace "\" with ""。现在,因为"{" to "}"中的每个字符串内容都由"," (comma)分隔,所以使用分隔符","(comma)拆分字符串。现在,您将获得包含每个"{" to "}" including both "{" and "}"内容的字符串数组。现在,您可以使用分隔符" " (single space)拆分它,以便获得包含从"c1 = 8847FF""moods_nam" = mood1

的每个元素的数组

但严重的是,将来使用这种方法会非常繁琐。考虑使用其他存储选项,例如Core Data,或者在sqlite中以更加可管理的格式存储它。