麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 19:52:50
字體:
來源:轉載
供稿:網友

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

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

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];

準備工作就完成了

-:UITableView的初始化

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

然后定義兩個對象

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

在.m文件里實現

@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]];    //實現代理    tableview.delegate = self;    tableview.dataSource = self;    //初始化數據    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數據源的初始化

UITableView有三個必須要實現的代理方法

#pragma mark - Table View//設置section的個數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}//設置每個section 的行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [dataArryList count];}//對每個TableViewCell進行操作  UITableView中顯示的每一個單元都是一個UITableViewCell對象,其初始化函數initWithStyle:reuseIdentifier:tableView快速滑動的滑動的過程中,頻繁的alloc對象是比較費時的///,于是引入了cell的重用機制- (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.    }}

四:其他的一些常用操作

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

最后看一下運行的效果

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品国产一区二区 | 狠狠操夜夜爱 | av免费在线观看国产 | 久久国产28| 久久精品视频首页 | 成人在线第一页 | 国产乱淫a∨片免费观看 | china对白普通话xxxx | 国产亚洲美女精品久久久2020 | 毛片一级片 | 日韩精品久久久久久久电影99爱 | 久久久久久99 | 999精品国产| 欧美日韩中文字幕在线视频 | 国产一区二区成人在线 | 美女在线观看视频一区二区 | 一区二区三区四区视频在线观看 | 麻豆视频观看 | 在线天堂中文字幕 | 日本a v免费观看 | av电影网在线观看 | 国产精品亚洲激情 | 国产一级毛片高清视频完整版 | 久久av免费| 91美女啪啪 | 黄色免费电影网址 | 九九热精品在线播放 | 国产女厕一区二区三区在线视 | 日本在线播放一区二区 | 久久精品视频日本 | 欧美一级欧美 | 91在线视频播放 | 激情97 | 黄视频网站免费在线观看 | 成人在线视频免费 | 国产91av视频 | 久久精品国产99国产精品澳门 | 羞羞羞羞视频 | 国产一级二级视频 | 欧美视频一区二区三区在线观看 | 久久久久女人精品毛片九一 |