溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

UITableView的使用和各種方法(帝王級別控件)

發(fā)布時間:2020-06-30 19:33:58 來源:網(wǎng)絡(luò) 閱讀:719 作者:Im劉亞芳 欄目:開發(fā)技術(shù)

文件類型

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
//    MainViewController *mainVC = [[MainViewController alloc] init];
//    UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
//    self.window.rootViewController = naviVC;
//    [mainVC release];
    
    //試圖控制器和導(dǎo)航控制器的初始化
    MainViewController *mainVC = [[MainViewController alloc] init];
    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
    self.window.rootViewController = naviVC;
    [mainVC release];
    [naviVC release];
    
    
    
    
    [self.window makeKeyAndVisible];
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end


MainViewController.h

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@property (nonatomic , retain)NSMutableArray *array;  //給tableView提供數(shù)組
@end

MainViewController.m

#import "MainViewController.h"
#import "SecondViewController.h"
//簽訂協(xié)議
@interface MainViewController ()<UITableViewDataSource , UITableViewDelegate>
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray arrayWithObjects:@"可口可樂", @"雪碧", @"奶茶", @"脈動", @"農(nóng)夫山泉", @"綠茶", @"冰糖雪梨", nil];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"UITableView";
    //UITableView的使用
//    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 310, 480) style:UITableViewStylePlain];
//    [self.view addSubview:tableView];
//    [tableView release];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    
    
    //實現(xiàn)tableView的協(xié)議方法
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //分割線的顏色
    tableView.separatorColor = [UIColor blueColor];
    //分割線的高度
    tableView.rowHeight = 40;
    //給tableView添加一個頂部view
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    view1.backgroundColor = [UIColor orangeColor];
    view.backgroundColor = [UIColor blueColor];
    tableView.tableHeaderView = view;    //頂部view
    tableView.tableFooterView = view1;     //底部view
    [view release];
    [view1 release];
    
    [self.view addSubview:tableView];
    [tableView release];
    //刷新數(shù)據(jù)
    [tableView reloadData];
}
//告訴tableView 每個分區(qū)(section) 顯示多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];//數(shù)組的行數(shù)
}
//每一行(row)要顯示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSIndexPath有兩個屬性,一個是section,一個是row
    //一個indexPath就代表一個cell的位置
    NSLog(@"%d",indexPath.row);
    
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"啊哈哈哈"];
//    //系統(tǒng)的cell,默認(rèn)有三個控件   ,一個p_w_picpathView ,兩label
//    cell.textLabel.text = @"雪碧供應(yīng)商攜款潛逃";
//    cell.detailTextLabel.text = @"具體情況不明";
//    return cell;
    
    
    //****************cell重用機制******************//
    //每當(dāng)tableView要顯示一個cell的時候,系統(tǒng)都會調(diào)用這個方法給tableView提供一個新的cell
    //每個tableView內(nèi)部都有若干個cell的重用池,每當(dāng)需要cell的時候,都去某一個重用池中取得一個cell
    //如果重用池中有cell,就直接使用.如果沒有就創(chuàng)建一個新的cell,給cell一個重用標(biāo)識,便于系統(tǒng)區(qū)分.
    //1.從重用池中嘗試獲取一個cell
    static NSString *sta = @"重用標(biāo)識";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sta];
    //2.判斷是否取得一個cell
    if(cell == nil){
        //如果取得的cell是nil,就創(chuàng)建一個新的cell
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sta] autorelease];
        NSLog(@"需要新的cell");
        
    }
    NSString *name = [self.array objectAtIndex:indexPath.row];
    //3.對cell重新賦值使用
    cell.textLabel.text = name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"section:%d, row:%d",indexPath.section,indexPath.row];
    return cell;
    
}
//返回多個section分區(qū)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"section:%d",section];
}
//設(shè)置分區(qū)頂部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
//自定義一個分區(qū)的頂部view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor yellowColor];
    return  [view autorelease];
}
//處理點擊cell的事件  -----記住,謝謝 ...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@", [self.array objectAtIndex:indexPath.row]);
    //初始化第二個頁面,并且推出其二個頁面
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // 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

SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController ()<UITableViewDataSource , UITableViewDelegate>
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"第二頁";
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView release];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sta = @"aaa";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sta];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:sta] autorelease];
        NSLog(@"需要新的cell");
    }
    cell.textLabel.text = @"蘋果公司發(fā)布會";
    cell.detailTextLabel.text = [NSString stringWithFormat:@"section:%d, row:%d", indexPath.section, indexPath.row];
    return cell;
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // 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





向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI