UITableView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.frame = CGRectMake( 0, 0, 375, 667);
    
    UITableView *baseTableView = [[UITableView alloc]initWithFrame:CGRectMake( 0, 30, 375, 667) style:UITableViewStyleGrouped];
    
    baseTableView.delegate = self;
    baseTableView.dataSource = self;
    
    [self.view addSubview: baseTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//設置有多少個Section 組 默認是1組
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

//設置每一個單元格的頭信息
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch ( section) {
        case 0:
            return @"第一組";
            break;
        case 1:
            return @"第二組";
        case 2:
            return @"第三組";
            
        default:
            break;
    }
    
    return @"";
}

//設置每一個單元格的尾信息
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"hello!";
}

//點擊某單元格之後響應的事件
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog( @"------ %li", [indexPath row] );
    return indexPath;
}

//設置每一組 有多少個單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  30;
}

//繪製Cell 一般爲繪製單元格的數據 如 圖片 文字 等
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //定義一個單元格在緩存裏的別名
    static NSString *identifer = @"findCell";
    
    //在tableview找可複用的單元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifer];
    
    if( cell == nil ){
        //把某個單元格存入緩存 並用別名存放 方便查找
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: identifer];
    }
    
    cell.frame = CGRectMake( 0, ([indexPath row]+1) * 300, 375, 100);
    
    //單元格索引 從0開始
    NSInteger row = [indexPath row];
    //設置主標題文字信息
    cell.textLabel.text =[NSString stringWithFormat: @"這是第%li行",row+1];
    //設置子標題文字信息
    cell.detailTextLabel.text= @"沒有什麼大驚小怪~";
    //設置子標題字體、字體大小
    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:10];

    //選中的樣式
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    //設置圖片信息
    cell.imageView.image = [UIImage imageNamed:@"3.png"];
    
    //cell.accessoryType = UITableViewCellAccessoryNone;//cell沒有任何的樣式
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個小箭頭,距離右邊有十幾像素;
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個藍色的圓形button;
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對號;
    cell.accessoryType = UITableViewCellEditingStyleDelete;
    
    return cell;
}

//設置每一個單元格的行高
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
相關文章
相關標籤/搜索