国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ios開發(fā)入門篇(三):UITableView簡介

2019-11-14 19:52:50
字體:
供稿:網(wǎng)友

  最近做項(xiàng)目又開始用到了uitableview,溫習(xí)之余,在這里把uitableview的用法分享一下,有不對的地方歡迎大家提出來。

廢話不多說,先創(chuàng)建一個工程,由于Xcode6,去除了創(chuàng)建工程時的空項(xiàng)目的選項(xiàng),我們繼續(xù)選擇single view application  在這里我們用不到main storyboard  先刪掉,創(chuàng)建一個類,繼承自

UINavigationController ,這里文件名字叫做HealthViewcont

然后在appdelegate里的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    return YES;

}

添加如下代碼:

   self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [self.window makeKeyAndVisible];    self.window.rootViewController = [[HealthViewcont alloc]init];

準(zhǔn)備工作就完成了

-:UITableView的初始化

1.在.h文件里實(shí)現(xiàn) UITableViewDataSource,UITableViewDelegate兩個代理協(xié)議,如果你這里繼承的時UITableView  可以不用寫

然后定義兩個對象

@PRoperty(nonatomic)UITableView* tableview; @property(nonatomic)NSMutableArray* dataArryList;

在.m文件里實(shí)現(xiàn)

@synthesize tableview;@synthesize dataArryList;

2.在viewdidload里添加如下代碼

- (void)viewDidLoad {    [super viewDidLoad];   //初始化一個tableview    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)];    [self.view addSubview:tableview];    [self.navigationBar setBackgroundColor:[UIColor redColor]];    //實(shí)現(xiàn)代理    tableview.delegate = self;    tableview.dataSource = self;    //初始化數(shù)據(jù)    dataArryList = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"334", @"445",@"667",@"779",@"123",nil]];    // Do any additional setup after loading the view, typically from a nib.  }

到這里初始化就完成了

二:UITableView數(shù)據(jù)源的初始化

UITableView有三個必須要實(shí)現(xiàn)的代理方法

#pragma mark - Table View//設(shè)置section的個數(shù)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}//設(shè)置每個section 的行數(shù)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [dataArryList count];}//對每個TableViewCell進(jìn)行操作  UITableView中顯示的每一個單元都是一個UITableViewCell對象,其初始化函數(shù)initWithStyle:reuseIdentifier:tableView快速滑動的滑動的過程中,頻繁的alloc對象是比較費(fèi)時的///,于是引入了cell的重用機(jī)制- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString* cellIdentifier = @"cell";        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if (cell== nil) {        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;    }    cell.textLabel.text=[dataArryList objectAtIndex:indexPath.row];    return cell;}

三:插入和刪除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.dataArryList removeObjectAtIndex:indexPath.row];        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        [self.dataArryList insertObject:@"456" atIndex:indexPath.row];        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.    }}

四:其他的一些常用操作

//設(shè)置UITableView行縮進(jìn)-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{   NSUInteger row = [indexPath row];   return row;}//設(shè)置cell行間隔的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 60;}//設(shè)置選中Cell的響應(yīng)事件-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}//設(shè)置選中的行所執(zhí)行的動作-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSUInteger row = [indexPath row];    return indexPath;}//設(shè)置劃動cell是否出現(xiàn)del按鈕,可供刪除數(shù)據(jù)里進(jìn)行處理-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {}//設(shè)置刪除時編輯狀態(tài)-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{}

最后看一下運(yùn)行的效果

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武强县| 肇州县| 安福县| 淅川县| 加查县| 临城县| 咸阳市| 铁岭县| 平潭县| 墨脱县| 突泉县| 青川县| 仲巴县| 岑巩县| 平舆县| 濮阳县| 乌审旗| 游戏| 湾仔区| 濮阳县| 孝义市| 杂多县| 鄂尔多斯市| 太原市| 大田县| 东兴市| 纳雍县| 吴旗县| 铁岭市| 伊宁县| 赤壁市| 遵义市| 沾化县| 梅河口市| 汉川市| 化德县| 安福县| 温宿县| 博湖县| 龙井市| 屏南县|