uitableview持续练习

来源:互联网 发布:三维游戏制作软件 编辑:程序博客网 时间:2024/06/05 17:04
<span style="color:#ff0000;">对于我而言比较容易犯的几个错误:</span>
<span style="color:#ff0000;">(1)uitableview没有拖线跟相应的界面联系;</span>
<span style="color:#ff0000;">(2)self.tableview.datasource = self;//不能设置</span>
<span style="color:#ff0000;">(3)plist文件名有可能写错。</span>
<span style="color:#ff0000;"></span>
<span style="color:#ff0000;">!!!最重要一点是:数组的大小,如返回_heros.count会是0,但是返回self.heros.count则正常的。</span>
<span style="color:#ff0000;">表示应该是一个@property等的调用机制的问题。(这个有点难发现)。</span>
////  MJViewController.m//  04-英雄展示////  Created by apple on 14-3-30.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "MJViewController.h"#import "MJHero.h"@interface MJViewController () <UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) NSArray *heros;@property (weak, nonatomic) IBOutlet UITableView *tableView;@end@implementation MJViewController- (void)viewDidLoad{    [super viewDidLoad];    }- (NSArray *)heros{    if (_heros == nil) {        // 初始化        // 1.获得plist的全路径        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];                // 2.加载数组        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];                // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中        NSMutableArray *heroArray = [NSMutableArray array];        for (NSDictionary *dict in dictArray) {            // 3.1.创建模型对象            MJHero *hero = [MJHero heroWithDict:dict];                        // 3.2.添加模型对象到数组中            [heroArray addObject:hero];        }                // 4.赋值        _heros = heroArray;    }    return _heros;}#pragma mark - 数据源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.heros.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil];    // 取出模型    MJHero *hero = self.heros[indexPath.row];        // 设置cell的数据    cell.textLabel.text = hero.name;    cell.detailTextLabel.text = hero.intro;    cell.imageView.image = [UIImage imageNamed:hero.icon];            // 设置cell右边指示器的类型//    if (indexPath.row % 2 == 0) {        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//    }    //    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//    cell.accessoryView = [[UISwitch alloc] init];        // 设置背景(背景view不用设置尺寸, backgroundView的优先级 > backgroundColor)    UIImageView *bgView = [[UIImageView alloc] init];    bgView.image = [UIImage imageNamed:@"buttondelete"];//    bgView.backgroundColor = [UIColor redColor];    cell.backgroundView = bgView;        UIView *selectedbgView = [[UIView alloc] init];    selectedbgView.backgroundColor = [UIColor greenColor];    cell.selectedBackgroundView = selectedbgView;        return cell;}#pragma mark - 代理方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 60;}@end

0 0
原创粉丝点击