天天看點

UITableView 及其傳值

準備工作

@property(nonatomic, tetain)NSMutableArray *arr

// 自定義初始化

- (instancetype)initWithNibName:(NSString *)nibNameOrNil  bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNile];
    if(self){
        self.arr = [NSMutableArray arrayWithObject:@"宋江", @"盧俊義", @"吳用", @"公孫勝", @"關勝", @"林沖", @"秦明" ,@"呼延灼" , @"花容",@"柴進", @"李應", @"朱仝",@"魯智深",@"武松",nil];
    }
    return self;
}


self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
self.title = @"表視圖";

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake:(, , self.view.frame.size.width, self.view.frame.size.height - ) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:tableView];

// 設定行高

tableView.rowHeight = ;

// tableView 的兩套代理方法
tableView.dataSource = self; 
// 有兩個必須實作的方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

tableView.delegate = self;



// 第一套協定方法

- (NSInterger)tableview:(UITableView *)tableView numberOfRowInSection:(NSInteger)section
{
    // 讓數組裡的元素個數與行數保持一緻
    // 先執行設定分區的方法, 後執行每區有多少行
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 建立cell
    // static的特點
     隻初始化一次
     如果沒有初始值, 預設是零
     知道程式結束, 才會消失
    static NSString *reuse = @"reuse";
    // 當cell顯示結束之後,會把cell統一fangdao重用池中, 等需要cell顯示了,先從重用池中找, 看有沒有閑置的cell, 如果有的話就用閑置的cell, 如果沒有在建立
    // cell的重用目的是為了節約建立的成本, 擁有有限的cell把所有的資料顯示出來
    // 給重用池先設定一個重用的标志,根據這個标志可以找到重用池
    // tableView通過重用的标志在重用池中尋找cell, 如果有閑置的cell, cell會儲存一個有效的cell對象位址, 如果沒有, cell裡面則是nil, 空
    UITableViewCell *cell = [tableView
     dequeueReusableCellWithIndentifier:reuse];
    if(!cell){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    // 對cell 進行指派 cell裡有三個預設的控件
    cell.textLable.text = self.arr[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"圖檔名"];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView;(UITableView *)tableView
{
    // 設定顯示的區數
    return ;
}

- (Nsstring *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 顯示每個區的頭标題
    return @"lishanshan";
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableview
{
    // 快速找個 區
    return @[@"0", @"2"];
}

// 第二套協定方法

- (void)tableView:(UITableView *)tableVIew didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 點選indexPath 所執行的方法
}
           

UITableView的界面傳值

// 從前往後傳
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UIImageView *imageView;


self.view.backgroundColor = [UIColor greenColor];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
self.title = @"視圖組";

UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - ) style:UITableViewStylePlain];

tableview.blackgroundColor = [UIclor grayColor];
[self.view addSubview:tableview];
[tableview release];
tableview.rowHeight = ;
// 設定代理人
tableview.dataSource = self;
tableview.delegate = self;

 self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"圖檔名"];

方法

- (NSinteger)tableView:(UITableView *)tableView numberOfRowsInsection:(NSInteger)section
{
    return self.arr.count;
}

- (NSInteger)numberOfsectionsIntableView:(UITableView *)table
{
    return ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSSting *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIndentifier:resuse] autorelease];
    }
    cell.textlable.text = self.arr[indexPath.row];
    cell.detailTextLabel.text = [NSString StringWithFormat:@"%ld", indexPath.section];
    cell.imageView.image = [UIImage imageNamed:@"圖檔名"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *secondAC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondAC animated:YES];
    SecondAC.str = self.arr[indexPath.row];
}