IOS開發之UITableView的奇技

來源:互聯網
上載者:User

標籤:

Biaoac

age:保密

sex:直男

性格:低調沉穩,乖張內涵

部落格背景:之前一直在使用UITableView,但是一直都只是初識,後來在不斷的使用中找到了很多之前沒有在意的東西,遂整理出來,當然,有很多還是看別人的部落格中提到的點,我把他重踩一遍;

 

1.點擊的時候顯示選中狀態,但狀態一直村在,必須在點擊下一個的時候取消選中狀態

 

點擊cell的時候調用- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;
//離開點擊時調用,
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

一般用法是在didSelectRowAtIndexPath方法中加入[tableView deselectRowAtIndexPath:indexPath animated:YES];即點擊cell時cell有背景色,如過沒有選中另一個,則這個cell背景色一直在,加入這句話效果是在點擊結束後cell背景色消失。

- (void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath2.


2.cell選中時的背景顏色(預設灰色,現在好像只有無色和灰色兩種類型了)

@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;

UITableViewCellSelectionStyleNone,

UITableViewCellSelectionStyleBlue,

UITableViewCellSelectionStyleGray,

UITableViewCellSelectionStyleDefault

 

重要屬性:

indexpath.row:行indexpath.section:組 

separatorColor:分割線的顏色separatorStyle:分割線樣式 

用來自訂頭部和尾部自訂view只需要設定高度,寬度設定無效

tableHeaderView;

tableFooterView;

table中展示的資料是從模型(或者其他來源-資料來源)中取得的,因此要更改表格式資料,只需要更改資料來源中得資料,並重新整理表格即可。(不要直接更改tableviewcell中textLabel中得text資料,這樣上下拖動tableview還是會顯示原來的資料)

UITableViewCell的常用屬性

accessoryType:cell右邊的指標

accessoryView:可自訂右邊的view

backgroundView:自訂背景view

backgroundColor//優先順序低於backgroundView

selectedBackgroundView

 

3.顯示右側索引

- (NSArray )tableView{      NSMutableArray *indexs = [[NSMutableArray alloc] init];      for (int i = 0; i < kHeaderTitle.count; i++) {              [indexs addObject:kHeaderTitle[i]];        }        return indexs;}

 

4.刪除操作

一般這種Cell如果向左滑動右側就會出現刪除按鈕直接刪除就可以了。其實實現這個功能只要實現代理方法,只要實現了此方法向左滑動就會顯示刪除按鈕。只要點擊刪除按鈕這個方法就會調用。-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)   editingStyle forRowAtIndexPath:(NSIndexPath )indexPath;
- (void)tableView:(UITableView )indexPath{        if (editingStyle == UITableViewCellEditingStyleDelete) {                    [_titleArray removeObject:_titleArray[indexPath.row]];                    [tableView deleteRowsAtIndexPaths:@[indexPath]                     withRowAnimation:UITableViewRowAnimationBottom];             } }

5.導覽列圖片展開放大

01還是建立控制器,控制器裡面建立tableView,初始化其必要的代理方法使能其正常顯示.
02初始化tableView的時候讓tableView向下位移(位移下來的那段放圖片):_tableView.contentInset = UIEdgeInsetsMake(backGroupHeight - 64, 0, 0, 0);
03初始化圖片,注意圖片的frame設定,載入在tableView上imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0,                    -backGroupHeight, kDeviceWidth, backGroupHeight)];imageBg.image = [UIImage imageNamed:@"bg_header.png"];[_tableView addSubview:imageBg];
04根據滑動時的位移量改變圖片的frame,改變navigationBar的透明度- (void)scrollViewDidScroll:(UIScrollView )scrollView{  CGFloat yOffset = scrollView.contentOffset.y;  CGFloat xOffset = (yOffset + backGroupHeight)/2;  if (yOffset < -backGroupHeight) {      CGRect rect = imageBg.frame;      rect.origin.y = yOffset;      rect.size.height = -yOffset;      rect.origin.x = xOffset;      rect.size.width = kDeviceWidth + fabs(xOffset)2;      imageBg.frame = rect;  }  CGFloat alpha = (yOffset + backGroupHeight)/backGroupHeight;  [self.navigationController.navigationBar setBackgroundImage:              [self imageWithColor:[[UIColor orangeColor] colorWithAlphaComponent:alpha]]               forBarMetrics:UIBarMetricsDefault];  titleLb.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:alpha];}
05渲染navigationBar顏色方法- (UIImage )imageWithColor:(UIColor )color{  //描述矩形  CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);  //開啟位元影像上下文  UIGraphicsBeginImageContext(rect.size);  //擷取位元影像上下文  CGContextRef content = UIGraphicsGetCurrentContext();  //使用color示範填充上下文  CGContextSetFillColorWithColor(content, [color CGColor]);  //渲染上下文  CGContextFillRect(content, rect);  //從上下文中擷取圖片  UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();  //結束上下文  UIGraphicsEndImageContext();  return currentImage;}

6.重新整理介面

   

No。1
.重新整理整個TableVIew[self.tableView reloadData];?注意:此處reloadData是重新整理整個UITableView,有時候,我們可能需要局部重新整理。比如:只重新整理一個cell、只重新整理一個section等等。這個時候在調用reloadData方法,雖然使用者看不出來,但是有些浪費資源。?NO.2
重新整理局部cellNSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationFade];注意:這是重新整理第一個section的第一個cell很方便的一種方法,雖然看上去,代碼變多了,但是很節省資源,盡量減少重新整理頻率,這也是在iOS開發中對UITableView的一種最佳化。
NO.3局部重新整理SectionNSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];[self.tableView reloadSections:indexSetwithRowAnimation:UITableViewRowAnimationFade];上面這段代碼是重新整理第0個section。
NO.4重新整理動畫重新整理UITableView還有幾個動畫:typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {UITableViewRowAnimationFade, //淡入淡出UITableViewRowAnimationRight, //從右滑入 // slide in from right (or out to right)UITableViewRowAnimationLeft, //從左滑入UITableViewRowAnimationTop, //從上滑入UITableViewRowAnimationBottom, //從下滑入UITableViewRowAnimationNone, // available in iOS 3.0UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupyUITableViewRowAnimationAutomatic = 100// available in iOS 5.0. chooses an appropriate animation style for you};

 

IOS開發之UITableView的奇技

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.