zoukankan      html  css  js  c++  java
  • objective-c 开发最简单的UITableView时数据进不去的问题

    今天在使用UITableView时遇到的问题,我在plist文件配置的数据进不去列表,以下是解决方案

    问题原因:plist文件root的type配置错误 

    如上图所示,博主是使用plist文件作为我的沙盒,if Root's Type 为array时

    1 self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath];
    2 
    3 self.simpleData=[NSArray arrayWithContentsOfFile:filePath];

    上面的第一行与第三行代码任选其一就可以

    如果type为Dictionary时

     NSMutableDictionary *data = [[NSMutableDictionary alloc]
                                    initWithContentsOfFile:filePath];

    利用这种方式将plist文件位置传进去就行。

    附带源码 

    //
    //  ViewController.h
    //  Table
    //
    //  Created by snow on 2017/12/25.
    //  Copyright © 2017年 snow. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UITableViewController
    
    @property (strong,nonatomic)NSArray *simpleData;
    
    @end
    //
    //  ViewController.m
    //  Table
    //
    //  Created by snow on 2017/12/25.
    //  Copyright © 2017年 snow. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"simpleData" ofType:@"plist"];
        //self.simpleDatas=[NSMutableArray arrayWithContentsOfFile:filePath];
        
        self.simpleData = [[NSArray alloc] initWithContentsOfFile:filePath];
     //  self.simpleData=[NSArray arrayWithContentsOfFile:filePath];
    //    NSMutableDictionary *data = [[NSMutableDictionary
    //                                  alloc]
    //                                 initWithContentsOfFile:filePath];
        
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [self.simpleData count];
        
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *identifierString = @"simpleTableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
        }
        NSInteger rowIndex = [indexPath row];
        NSDictionary *cellDictionary = [self.simpleData objectAtIndex:rowIndex];
        cell.textLabel.text = [cellDictionary objectForKey:@"name"];
        cell.imageView.image = [UIImage imageNamed:[cellDictionary objectForKey:@"cover"]];
        
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        return cell;
    }
    
    
    @end
  • 相关阅读:
    iOS开发UIKit框架-可视化编程-XIB
    ios swift例子源码网址总结
    两个单链表相交问题
    库函数atoi
    C++类型萃取
    【Swift】iOS导航栏错乱的原因
    【Swift】iOS裁剪或者压缩后出现的白边问题
    Linux进程调度分析
    【scala】可变与不可变的理解
    【scala】模式匹配
  • 原文地址:https://www.cnblogs.com/jianwind/p/8118759.html
Copyright © 2011-2022 走看看