IOS——中級篇,ios
// 設定tableView的行高
self.tableView.rowHeight = 100;
// 設定tableView分割線的樣式
// UITableViewCellSeparatorStyleNone 不顯示分割線
// UITableViewCellSeparatorStyleSingleLine 顯示分割線(預設) self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; self.tableView.allowsSelection = NO; // 不允許選中// 設定分割線的顏色
self.tableView.separatorColor = [UIColor orangeColor]; // 讓沒有內容儲存格不顯示[小技巧] self.tableView.tableFooterView = [[UIView alloc] init];// 設定(top,left,bottom,right)[top與bottom無效] self.tableView.separatorInset = UIEdgeInsetsMake(0,10, 0, 10);
在stroyborud 載入cell CZFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friend"]; return cell;
dataSource 常用方法/** * 多少個分組 numberOfSectionsInTableView */
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 2;
}
/** * 一個分組有多少行 numberOfRowsInSection */
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return 2;}
/** * 每一個分組顯示什麼內容 cellForRowAtIndexPath */
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //1建立Cellstatic NSString *reusedId = @"item";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId forIndexPath:indexPath];--------->wrong!!!!!
UITableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:reusedId ];
if (cell ==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId]; }// 2.擷取資料 LYGroup *group = _groups[indexPath.section];
LYItem *item = group.items[indexPath.row];
cell.textLabel.text =item.title; cell.imageView.image =[UIImage imageNamed:item.icon];//3.返回cell return cell;}
/** * 分組(頭部)標題 titleForHeaderInSection */
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @“頭部”;}
/** * 分組(尾部)描述 titleForFooterInSection */
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{ return @“尾部”;}/** * 分組索引 sectionIndexTitlesForTableView */
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
//返回groups數組中,所有group對象的title屬性,返回的是數組
return [self.carGroups valueForKeyPath:@"title"];}
代理常用方法#pragma mark - 代理方法
/** * 設定每一行的行高 heightForRowAtIndexPath */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
return 60;
}else{
return 100;
}
}
/** * 已經選中某一行 didSelectRowAtIndexPath */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// zd long, long long ,int
NSLog(@"使用者選中了第%zd組,第%zd行",indexPath.section,indexPath.row);
}
//當取消選中某一行時候執行 didDeselectRowAtIndexPath- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"使用者取消選中了第%zd組,第%zd行",indexPath.section,indexPath.row);}
CELL 常用屬性// UITableViewCell *cell = [[UITableViewCell alloc] init];// UITableViewCellStyleDefault 只顯示表徵圖和名稱
// UITableViewCellStyleSubtitle 顯示表徵圖,名稱,描述<下面>
// UITableViewCellStyleValue1 顯示表徵圖,名稱,描述<後面>// UITableViewCellStyleValue2 顯示名稱,描述<後面>// 背景視圖color UIView *backView = [[UIView alloc] init];
backView.backgroundColor = [UIColor redColor];
// backgroundView的優先順序高於backgroundColor
cell.backgroundView = backView;
// 背景顏色
cell.backgroundColor = [UIColor blueColor];
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor blueColor];
// 選中背景視圖
cell.selectedBackgroundView = selectedView;
// 指標相關
// 設定指標的類型
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 指標視圖 cell.accessoryView = [[UISwitch alloc] init];/** * 從緩衝中取cell //1建立Cell // 2.擷取資料 //3.返回cell */// 定義重用標識
static NSString *reuseId = @"heroCell";
// 去緩衝池中尋找重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
// 如果沒有找到可以重用cell就建立新的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
// NSLog(@"建立cell"); }
// 用來重新整理指定的行 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; // 重新整理表格[self.tableView reloadData];
// 滾動讓某個地區可見[self.tableView scrollRectToVisible: (CGRect)rect animated:YES];
//滾動到哪一行NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tgs.count -1 inSection:0]; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES]; // 第一組和最頂部的間距 headerView self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)]; // 設定 組間距 15 self.tableView.sectionHeaderHeight = 15;
self.tableView.sectionFooterHeight = 0;
//設定tableView的背景圖片
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]]; //重寫init方法,設定分組
return [super initWithStyle:UITableViewStyleGrouped]; UITableViewHeaderFooterView重寫 [[self alloc] initWithReuseIdentifier:reuseId];UITableViewCell重寫 [[self alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];