zhaoguodongios的专栏

打开UIViewController.h

//

// RootViewController.h

// Lesson09TableView

//

// Created by Dubai on 14-9-26.

// Copyright (c) 2014年 Dubai All rights reserved.

//

#import <UIKit/UIKit.h>

//遵循一下代理

@interface RootViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>

@end

打开 UIViewController.m为:

//

// RootViewController.m

// Lesson09TableView

//

// Created by Dubai on 14-9-26.

// Copyright (c) 2014年 Dubai All rights reserved.

//

#import "RootViewController.h"

@interfaceRootViewController ()

@end

@implementation RootViewController

– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

returnself;

}

– (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view.

allocstyle:(UITableViewStyleGrouped)];

[self.viewaddSubview:tableView];

[tableViewrelease];

//属性

tableView.rowHeight =90;//行高

tableView.

//tableView.separatorStyle = UITableViewScrollPositionNone;//分割线消失

//tableView.separatorStyle = UITableViewScrollPositionBottom;

//tableView.separatorStyle = UITableViewRowAnimationRight;

tableView.dataSource =

tableView.delegate =self;//负责控制的代理对象

}

//设置分区,可选实现,因为tableview默认有一个分区(数据源代理)

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 4;

}

//设置每个分区的行数,必须实现(数据源)

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 5;

}

//设置每行要显示的内容,每行所在位置会放值一个tabelViewcell,每行要显示的数据,设置在cell上必须实现

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//当某行移出屏幕时,tableView会将这行显示的cell,移动到重用集合中储存,这行就咩有cell显示任何数据.

//因此,只要某行要进入屏幕显示,必须执行这个代理方法.设置这行要显示的cell;

NSLog(@"row= %ld",indexPath.row);

//indexPath包含两个属性 section和 row即分区和行数

//section 和row 的索引都从0开始.每个分区中的row的索引都是从0开始

alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier

//cell的样式是用来影响3个视图的位置

//cell.imageView.image = [UIImage imageNamed:@"ha.png"];

//cell.detailTextLabel 直接用 不用创建(不能改变大小)

cell.第几行

=)

//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//辅助视图

cell.accessoryType =UITableViewCellAccessoryCheckmark;

//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//只让第一行显示checkmark

if (indexPath.row ==0) {

//如果是第一行就显示为checkmark

cell.accessoryType =UITableViewCellAccessoryCheckmark;

}else{

cell.accessoryType =UITableViewCellAccessoryNone;

}

return cell;

// //重用

//

// //先从重用队列中获取可以被重用的cell对象,

//

// static NSString *indentifier = @"cell";//标示

// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];

// //如果重用队列中没有可以使用的cell,必须自己创建.

// if (cell == nil) {

// cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:indentifier] autorelease];

// NSLog(@"创建的新的cell对象");//只许创建对象

//

//// cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

//

// }

// //重用在这里

// //设置当前要使用的cell,可能放置在任何一行

// cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

// //cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];

// cell.detailTextLabel.text = [NSString stringWithFormat:@"nihao"];//显示不显示跟上面的cell创建时的subtitle有关

// return cell;

}

//给每个分区设置头部标题

– (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [NSStringstringWithFormat:@"%ld",section +1];

}

//给所有的副标题右侧的.

– (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

@"1",@"2",@"3"];

}

////设置行高

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//{

// if (indexPath.section == 0) {

// return 20;

// }if (indexPath.section == 1) {

// return 60;

// }if (indexPath.section == 2) {

// return 40;

// }if (indexPath.section == 3) {

// return 100;

// }return 2;

//

//

//}

//

//检测cell被选中的第几个分区第几行

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

,indexPath.section+1,indexPath.row );

}

– (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

如图:

贪婪是最真实的贫穷,满足是最真实的财富

zhaoguodongios的专栏

相关文章:

你感兴趣的文章:

标签云: