iOS UI笔记-TableView-03-例子

Part1 —— 设置行样式

1、设置UITableViewCell的四种样式 UITableViewCell Style | Subtitle | Value1 | Value2

2、添加辅助图标,实现代理方法

3、完成一个分组的表视图

4、设置单元格的背景颜色和选中颜色

 

Part2 —— 单元格高度自适应 +  表视图的单选

 步骤:

1、新建Root05ViewController,继承UIViewController, 实现UITableViewDatasource和UITableViewDelegate协议, 添加_tableView (UITableView) 和 _dataArray (NSArray)两个属性,override协议UITableViewDatasource的三个基本方法,覆盖loadView和 dealloc两个方法。

 

@interface Root05ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
@private UITableView *_tableView;
    NSArray     *_dataArray;
}
@end

 

   

- (void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = view;
    [view release];
    _dataArray = [@[@"新闻中心 > 国内新闻 > 正文",@"XXX收独龙族乡亲来信 祝贺独龙江隧道将贯通",
                   @"新华网北京1月3日电  2014年元旦前夕,云南省贡山独龙族怒族自治县干部群众致信XXX,汇报了当地经济社会发展和人民生活改善情况,重点报告了多年期盼的高黎贡山独龙江公路隧道即将贯通的喜讯。收到来信后,XXX立即作出重要批示。",
                   @"XXX指出:获悉高黎贡山独龙江公路隧道即将贯通,十分高兴,谨向独龙族的乡亲们表示祝贺!独龙族群众居住生活条件比较艰苦,我一直惦念着你们的生产生活情况。希望你们在地方党委和政府的领导下,在社会各界帮助下,以积极向上的心态迎战各种困难,顺应自然规律,科学组织和安排生产生活,加快脱贫致富步伐,早日实现与全国其他兄弟民族一道过上小康生活的美好梦想。",
                  
                   @"独龙族是我国人口较少的少数民族之一,只有4000多人口,主要聚居在云南省怒江州贡山县独龙江乡。独龙江乡深处峡谷,自然条件十分恶劣,仅有一条独龙江公路通往外界,每年有半年大雪封山、与世隔离,经济社会发展滞后,一直是云南乃至全国最为贫穷落后的地区。XXX对独龙族等人口较少民族地区扶贫工作十分关心,多次作出重要指示,提出明确要求。近年来,YYY认真落实XXX重要指示精神,大力推动独龙江乡扶贫开发工作不断深入,使当地基础设施和群众生产生活不断改善。",
                  
                   @"独龙江公路是独龙族与外界联系沟通的唯一通道,是独龙族同胞生产生活和经济发展的命脉,尤其是公路中途的41公里至63公里的高黎贡山独龙江公路隧道是整条公路建设的瓶颈。公路隧道的贯通,标志着独龙族同胞祖祖辈辈大雪封山半年的历史宣告结束,有助于大幅提升独龙族同胞的生产生活水平。"] retain];
    _tableView = [[UITableView alloc] initWithFrame:view.bounds];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *content = _dataArray[indexPath.row];
    CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 1000) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height + 20;/*这里再加20是为了避免出现...的现象*/
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"mycell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    cell.textLabel.text = _dataArray[indexPath.row];
    cell.textLabel.numberOfLines = 0;/*numberOfLine=0表示可以多行,默认是1行*/
    cell.textLabel.font = [UIFont systemFontOfSize:15];/**这里字体大小最好设置成和heightForRowAtIndexPath中一样的大小**/
    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)dealloc
{
    [_tableView release];
    _tableView = nil;
    [_dataArray release];
    _dataArray = nil;
    [super dealloc];
}

 

步骤2:给Root05增加_selectedIndex(int) 属性,在loadView方法中将其置为-1;

            在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中增加单选的逻辑判断

           

/*增加单选的标签*/
    if(_selectedIndex == indexPath.row){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;

         实现协议的didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  NSIndexPath -> max row  取消上一次选中
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
    lastCell.accessoryType = UITableViewCellAccessoryNone;
    // 用户选中了新的一行
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    _selectedIndex = indexPath.row;
   // [_tableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:indexPath afterDelay:.5];
}

 

Part4 —— 创建索引表视图

 

 

 

猜你喜欢

转载自zhengjj-2009.iteye.com/blog/1998777
今日推荐