iOS開發UItableview的常用屬性方法的使用

來源:互聯網
上載者:User

標籤:搜尋   selectrow   self   capacity   wan   cti   news   lin   indicator   

有些屬性和方法始終是記不清,只能記下來,方便尋找

如果對你有協助請支援,沒有協助請告訴我哪裡需要改進!謝謝!

//  ViewController.m

//  TableViewAll

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    [self setupTableview];

}

- (void)setupTableview

{

    UITableView *myTblView = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 40) style:UITableViewStylePlain];

    //UITableViewStylePlain,                  // 標準的表視圖風格

    //UITableViewStyleGrouped                 // 分組的表視圖風格

    myTblView.delegate = self;

    myTblView.dataSource = self;

    //tableview背景色

    myTblView.backgroundColor = [UIColor brownColor];

    

    //cell分割線只顯示右邊一半(設定cell分割線的長度)

    myTblView.separatorInset=UIEdgeInsetsMake(0, myTblView.frame.size.width/2, 0,0);

    

    //cell的分割線的顏色不是cell的屬性,它屬於tableView的separatorColor屬性。

    [myTblView setSeparatorColor:[UIColor redColor]];

    

    //取消cell的分割線

    //    myTblView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //UITableViewCellSeparatorStyleSingleLine  系統預設的

    //UITableViewCellSeparatorStyleSingleLineEtched  此分隔樣式只支援當前分組樣式表視圖(用的比較少)

    

    //取消tableView右側的捲軸

    myTblView.showsVerticalScrollIndicator = NO;

    

    //設定tableView的位移量

    //    [myTblView setContentOffset:CGPointMake(0, 100) animated:YES];

    //當tableview資料較少時,動態隱藏tableView的底部線條

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

    

    //隱藏tableView的footerView

    myTblView.sectionFooterHeight = 0;

    

    [self.view addSubview:myTblView];

}

//在tableView索引中顯示搜尋按鈕

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

{

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

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

    return arr;

}

//cell點擊觸發方法

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

{

    //tableView選中時反選

    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //cell的數量

    return 10;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //cell高度

    return 30;

}

//cell預設背景色的修改

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

    cell.backgroundColor = [UIColor grayColor];

}

//設定cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *ident = @"shool";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];;

    }

    //cell選中時的背景色

    UIImageView *imageView = [[UIImageView alloc]init];

    imageView.backgroundColor = [UIColor yellowColor];

    cell.selectedBackgroundView = imageView;

    cell.backgroundColor = [UIColor whiteColor];

    /*

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

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

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

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

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

     */

    return cell;

}

/*

 擷取多選cell的位置資訊

 - (NSArray *)indexPathsForSelectedRows;

 

 tableview的編輯(屬性類)

 設定是否是編輯狀態(編輯狀態下的cell左邊會出現一個減號,點擊右邊會划出刪除按鈕)

 @property (nonatomic, getter=isEditing) BOOL editing;

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;

 

 設定cell是否可以被選中(預設為YES)

 @property (nonatomic) BOOL allowsSelection;

 

 設定cell編輯模式下是否可以被選中

 @property (nonatomic) BOOL allowsSelectionDuringEditing;

 

 設定是否支援多選

 @property (nonatomic) BOOL allowsMultipleSelection;

 

 設定編輯模式下是否支援多選

 @property (nonatomic) BOOL allowsMultipleSelectionDuringEditing;

 

 通過位置路徑擷取cell

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

 

 

 

 

 

 tableview插入分區

 - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

 刪除分區

 - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

 重載一個分區

 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;

 移動一個分區

 - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;

 插入一些行

 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 刪除一些行

 - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 重載一些行

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 移動某行

 - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

 animation參數是一個枚舉,枚舉的動畫類型如下

 UITableViewRowAnimationFade,//淡入淡出

 UITableViewRowAnimationRight,//從右滑入

 UITableViewRowAnimationLeft,//從左滑入

 UITableViewRowAnimationTop,//從上滑入

 UITableViewRowAnimationBottom,//從下滑入

 UITableViewRowAnimationNone,  //沒有動畫

 UITableViewRowAnimationMiddle,

 UITableViewRowAnimationAutomatic = 100  // 自動選擇合適的動畫

 

 */

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

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.