UITableViewController根视图控制器——用IB实现

1.删除ViewController

2.拖拽TableViewController

3.设置TableViewController为初始视图控制器

ViewController➡️IsInitialViewController复选框打☑️

4.修改ViewController父类为UITableViewController

ViewController这一步设置了其所属类,这样就可以在ViewController中对UIViewTableController进行设置了

5.Scene列表中选择TableViewController,打开其标示检查器,将Class选择ViewController

只有完成第4步,才能完成这一步,这一步是为了把原本app目录中的ViewController定义为TableViewController

ViewController只是TableViewController的改名,虽然是改名,但是必须要做,这样做就不用再新建一个TableViewController类了

6.设置可重用单元格标示符

1.设置cell的样式和标识符

//不需要这一步了,因为ViewController已经继承了TableViewControlle,因为UITableViewControlle已经实现了协议,分配了属性

2.在ViewController.h中加<UITableViewDataSource,UITableViewDelegate>

3.在ViewController.m中加@property(nonatomic,strongNSArray *listTeams;

4.在ViewController.m中加

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"team" ofType:@"plist"];//文件路径
    self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath]; //文件数据数组  
}

5.

//返回某个节点的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.listTeams count];
}
//为单元格提供数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //只需要填写一个值@"CellIdentifier" ,这个值是之前自己设置的cell名称
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    NSUInteger row = [indexPath row];//得到了数组的索引
    NSDictionary *rowDict = self.listTeams[row];//数组的索引就是字典
    

cell.textLabel.text = rowDict[@"name"];//单元格的主标题文字

    cell.detailTextLabel.text = rowDict[@"image"];//单元格的辅标题文字

NSString *imagePath = [[NSString alloc] initWithFormat:@"%@.png",rowDict[@"image"]];//图片路径 cell.imageView.image = [UIImage imageNamed:imagePath];//设置图片 //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//设置cell的类型 return cell;}

猜你喜欢

转载自blog.csdn.net/sndongcheng/article/details/81047882