ios tableView基本屬性一:

ios tableView基本屬性一:

//返回的section數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//返回每個section的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

//返回的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellindex = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindex forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellindex];
    }
    cell.imageView.image = [UIImage imageNamed:@"[email protected]"];
    cell.textLabel.text = @"123456789";
    
    tableView.allowsSelection=YES;
    //cell被選中後的狀態
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //設置背景顏色
    cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
    //如何設置tableview 可以被編輯,首先要進入編輯模式: 
    [tableView setEditing:YES animated:YES];
    return cell;
}

//返回指定的headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 30, 50, 60)];
    view.backgroundColor = [UIColor redColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//這個方法返回指定的 row 的高度。
    return 100;
}

//這個方法返回指定的 section的footer view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

//返回指定的 section 的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

//返回指定的section的 title,如果這個section.header有返回view,那麼title就不起作用了。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"哈哈哈";
}

//當用戶選中某個行的cell的時候,回調用這個。但是首先,必須設置tableview的一個屬性爲可以select 纔行。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"你選擇了%@行",indexPath);
}

//返回當前cell  要執行的是哪種編輯,下面的代碼是 返回刪除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章