tableView基本用法于常用屬性

一、tableView的屬性

1.取消cell的分割線

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

2.取消tableView右側的滾動條

tableView.showsVerticalScrollIndicator = NO;

3.當tableview數據較少時,動態隱藏tableView的底部線條

tableView.tableFooterView = [[UIView alloc]init];

4.設置tableView的偏移量

[myTableView setContentOffset:CGPointMake(0, 100)? ? animated:YES];

5.隱藏tableView的footerView

tableView.sectionFooterHeight = 0;

6.tableView選中時反選

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

8.在tableView索引中顯示搜索按鈕

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];

[arr addObject:@"{search}"];//等價于[arr addObject:UITableViewIndexSearch]; return arr;

}

二、Cell的屬性

1.設置單元格選中時的背景色

系統默認的顏色設置

//無色

cell.selectionStyle = UITableViewCellSelectionStyleNone;

//藍色

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

//灰色

cell.selectionStyle = UITableViewCellSelectionStyleGray;

方法一、

UIImageView *imageView = [UIImageView alloc]init];

imageView.backgroundColor = [UIColor clearColor];

cell.selectedBackgroundView = imageView;

方法二、

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];

UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];

bgview.opaque = YES;

bgview.backgroundColor = [UIColor orangeColor];

[cell setBackgroundView:bgview];

2.設置單元格默認背景色

通過屬性設置

cell.contentView.backgroundColor = [UIColor redColor];

通過方法設置

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

cell.backgroundColor = [UIColor redColor];

}

3.取消單元格選中時背景色

cell.textLabel.highlightedTextColor = [UIColor redColor];

4.調整單元格之間的距離

- (void)setFrame:(CGRect)frame

{

// tableView邊框的寬度

#define kTableBorderWidth 5

// 更改x、寬度

frame.origin.x = kTableBorderWidth;

frame.size.width -= kTableBorderWidth * 2;

// 更改頂部間距、每個cell之間的間距

frame.origin.y += kTableTopBorderWidth;

frame.size.height -= kTableViewCellMargin;

[super setFrame:frame];

}

5.單元格的屬性

cell.accessoryType =UITableViewCellAccessoryNone;//cell沒有任何的樣式

cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個小箭頭,距離右邊有十幾像素;

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個藍色的圓形button;

cell.accessoryType =UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對號;

cell.selectionStyle =UITableViewCellSelectionStyleNone;//cell選中狀態的樣式,為枚舉類型

6.tableView的cell之間的分割線長度改變方法

self.settingTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

self.settingTableView.separatorInset=UIEdgeInsetsMake(0, 40, 0, 20);

7.隱藏UITableView多余的分割線

- (void)setExtraCellLineHidden: (UITableView *)tableView

{

UIView *view =[ [UIView alloc]init];

view.backgroundColor = [UIColor clearColor];

[tableView setTableFooterView:view];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容