天天看點

UITableView背景,邊框線設定

UITableViewStylePlain類型的UITableView去除邊框線有直接的屬性方法:

tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

在UITableViewStyleGrouped類型的UITableView中

separatorColor=[UIColor clearColor];

1.去掉分隔線,背景:

   去掉分隔線:

   tableView.separatorStyle=UITableViewCellSeparatorStyleNone; //在syle為UITableViewStylePlain有效,隐藏了分隔線,但在UITableViewStyle為UITableViewStyleGrouped時不起作用.

   或者用tableView.separatorColor=UIColor.clearColor;

   去掉背景:

   tableView.backgroundColor =  UIColor.clearColor;

   tableViewCell.backgroundColor=UIColor.clearColor;

  注意:在UITableViewStyle為UITableViewStyleGrouped時,tableView所占的高度大于行高的和。

   tableView.scrollEnabled = NO;  禁用滾動

2.設定邊粗細及顔色:

self.layer.borderWidth=4.0;

self.layer.borderColor=[[UIColor redColor]CGColor];

3.基本使用方法

http://blog.csdn.net/tangaowen/archive/2011/05/22/6438362.aspx

UITableView每一行的高度預設是44,

4.在UITableView.h頭檔案中,對NSIndexPath增加了類别,增加了section,row方法,分别擷取選擇的列索引,行索引。

   @interface NSIndexPath (UITableView)

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

@property(nonatomic,readonly) NSUInteger section;

@property(nonatomic,readonly) NSUInteger row;

@end

5. 給UITableViewCell的成員設定架構屬性後不起作用,解決辦法是在UITableViewCell的子類中重寫layoutSubviews,在其中改變一些屬性的值,例如下:

- (void)layoutSubviews {

    [super layoutSubviews];

    self.imageView.bounds = CGRectMake(0,0,75,75);

    self.imageView.frame = CGRectMake(0,0,75,75);

    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    CGRect tmpFrame = self.textLabel.frame;

    tmpFrame.origin.x = 77;

    self.textLabel.frame = tmpFrame;

    tmpFrame = self.detailTextLabel.frame;

    tmpFrame.origin.x = 77;

    self.detailTextLabel.frame = tmpFrame;

}

http://stackoverflow.com/questions/3130804/changing-bounds-of-imageview-of-uitableviewcell

6.UITableViewCell.selectionStyle設定標明時的單元格顔色。

7.重用單元格:測試發現在cellForRowAtIndexPath中,會alloc螢幕所顯示的數量+1個UITableViewCell,并且顯示每個單元格都會進入UITableViewCell函數調用。

   重寫- (void)prepareForReuse;                                                       // if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:.  If you override, you MUST call super.

  單元格重用是重用它的記憶體配置設定,注意更新它的内容,以防内容出現混亂。  

8.設定UITableViewCell的背景色将看不到效果。

   可以先在window對象中添加圖檔視圖,再添加透明背景的表格視圖,以透出背景。

9.四種UITableViewCellStyle

    UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)

    UITableViewCellStyleValue1,  // Left aligned label on left and right aligned label on right with blue text (Used in Settings)

    UITableViewCellStyleValue2,  // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)

    UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).

     其中UITableViewCellStyleValue2 不支援cell.imageView。

    四種通路類型UITableViewCellAccessoryType:

    UITableViewCellAccessoryNone,                   // don't show any accessory view

    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track

    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks

    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track

    其中 UITableViewCellAccessoryDetailDisclosureButton 是藍色V型的按鈕,它響應按鈕點選;

            UITableViewCellAccessoryDisclosureIndicator 是灰色V型訓示器,它響應單元格操作,導向子菜單,

    例:cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1

                                       reuseIdentifier:CellIdentifier] autorelease];

           cell.textLabel.text = @"字型大小";

           cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

           [email protected]"大";

還可以自定義設定accessoryView,替換accessoryType的内置效果。

例如顯示系統的加号圖示按鈕:cell.accessoryView=[UIButton buttonWithType:UIButtonTypeContactAdd];

10.可以繼承UITableViewCell來建立自定義的cell。

11.主動移除單元格選中狀态:

    可以在didSelectRowAtIndexPath中延遲執行 [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];實作。

12.删除單元格:設定[tableview setEditing:YES];表格為編輯狀态。

     滑動删除功能。

     删除确認後,進入commitEditingStyle回調函數。

13.移動表格:

    在UITableViewController中提供了moveRowAtIndexPath方法。然後在其中寫移除和插入的方法。

14.表格排序是對數模型源排序,然後reloadData。

15.《秘籍2》11.17中介紹了搜尋表格。

      UISearchBar和 UISearchDisplayController的使用

      http://www.cnblogs.com/mobiledevelopment/archive/2011/08/04/2127633.html

16.索引功能,分組樣式

     《秘籍2》11.18 分段加索引功能。帶有索引的表格特征之一就是拖動時段頭浮動保持。

         分組樣式是分段的進一步的展現形式。但蘋果公司建議不要使用帶有分組表格的分段索引。這樣右邊會顯的淩亂。

     還可以定制表頭和腳注。

17.UIPickerView:

     要從很長的清單中,或同時從多個表格中挑選,用UIPickerView。

     它不适合作為應用程式的焦點,是以沒有UIPickerViewController類。可以把它放到UIActionSheet中,這可以友善的提供确定,取消按鈕。

     它使用數字,而不是NSIndexPath建立索引。

      它的大小固定:縱向320x216,橫向480x162,這與标準iPhone鍵盤尺寸相同。

      iPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];

      CGSize pickerSize = [iPickerView sizeThatFits:CGSizeZero];

     沒有循環設定屬性,有的實作設定為很多行,模拟看起來像循環。

    UIDatePicker内部使用了UIPickerView。

18. heightForRowAtIndexPath先于cellForRowAtIndexPath執行。

19.UITableViewDataSource

//設定某行是否可編輯,如果有的行可編輯,有的不可編輯,會出現隻有部分行向右移動的情況。但不影響功能。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

{

    int section=[indexPath section];

    if(section==1)

    {

        return NO;

    }

    if ([indexPath row]==0) 

    {

        return NO;

    }

    return YES;

}

 cell.imageView.image=image;//未選cell時的圖檔

 cell.imageView.highlightedImage=highlightImage;//選中cell後的圖檔

//設定讓UITableView行縮進

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ 

 NSUInteger row = [indexPath row]; 

 return row; 

//傳回目前所選cell 

NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section]; 

[TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone]; 

//設定UITableView的style 

[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone]; 

//設定選中Cell的響應事件

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

 [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中後的反顯顔色即刻消失

//設定選中的行所執行的動作

-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    NSUInteger row = [indexPath row]; 

     return indexPath; 

//設定劃動cell是否出現del按鈕,可供删除資料裡進行處理

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath { 

//設定删除時編輯狀态

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 

forRowAtIndexPath:(NSIndexPath *)indexPath 

//右側添加一個索引表

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

(2)  其他

//選中cell時的顔色,在官方文檔有如下可以選擇

typedef enum { 

    UITableViewCellSelectionStyleNone, 

    UITableViewCellSelectionStyleBlue, 

    UITableViewCellSelectionStyleGray 

} UITableViewCellSelectionStyle 

//cell右邊按鈕格式

typedef enum { 

    UITableViewCellAccessoryNone,                   //don't show any accessory view 

    UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn't track 

    UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks 

    UITableViewCellAccessoryCheckmark               //checkmark. doesn't track 

} UITableViewCellAccessoryType 

//是否加換行線

typedef enum { 

    UITableViewCellSeparatorStyleNone, 

    UITableViewCellSeparatorStyleSingleLine 

} UITableViewCellSeparatorStyle 

//改變換行線顔色

tableView.separatorColor= [UIColor blueColor]; 

4.    UITableViewCell

表中的每一行都代表一個UITableViewCell。可以使用圖像、文本還有輔助的圖示等來自定義你自己的UITableViewCell。你可以自定義你自己的cell如下模型或者像appstore那樣的。

UITableView背景,邊框線設定

UITableViewCell為每個Cell提供了三個可以選擇的屬性,如下:

l textLabel:填寫文本

l detailTextLable:稍微詳細的副标題

l imageView:用來顯示你cell的圖檔,可以通過UIImage來加載。