UITableView,UITableViewCell,UIPickerView,UISearchBar

來源:互聯網
上載者:User

1.UITableView 去掉分隔線,背景:

   去掉分隔線:

   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.UITableView 基本使用方法

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設定選定時的儲存格顏色。

     還可以直接設定選中時的文字顏色 highlightedTextColor,記住不是selectedTextColor

    cell.textLabel.textColor = ...;
    cell.textLabel.highlightedTextColor = ...;

    cell.detailTextLabel.highlightedTextColor = ...;

   

       設定選中時的背景或背景顏色:

       cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
       cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];

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;
           cell.detailTextLabel.text=@"大";

還可以自訂設定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;
}

20. //在點擊編輯按鈕時每行是否有縮排
    cell.backgroundColor=UIColor.clearColor;
    UIImageView* bg=[[UIImageView alloc] initWithImage:[self cellBgImageWithSection:section row:row]];
    cell.backgroundView=bg;
    [bg release];
    cell.textLabel.text = text;
 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
 {
     //這個只響應groups形式的tableView
     return NO;
 }

效果是,刪除按鈕在bg上面的左部淡出顯示,同時textLabel向右移動。

   在ios4中,[cell addSubview:customView];將會支援編輯時向右移動的效果;但在ios6中[cell.contentView addSubview:customView];才支援。

21.UISearchBar

    CGRect searchFrame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    iSearchBar = [[UISearchBar alloc] initWithFrame:searchFrame];
    iSearchBar.delegate = self;
    [[iSearchBar.subviews objectAtIndex:0] removeFromSuperview];//去掉內建的周邊背景
    [self.view addSubview:iSearchBar];

    第一次彈出鍵盤時慢,怎麼解決?

22. 一種私人風格的複選 

       在實現了 UITableViewDelegate 的類中實現下面的方法

 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return 3;
}

其中 UITableViewCellEditingStyle 的定義在 SDK 中如下所示: typedef enum {
   UITableViewCellEditingStyleNone,
   UITableViewCellEditingStyleDelete,
   UITableViewCellEditingStyleInsert
} UITableViewCellEditingStyle

另外取得被選擇的item可以使用 indexPathsForSelectedRows 方法,該方法也也是私人的。也可以使用公開的方法來一個一個的選擇/解除cell,而不用使用私人API。

      http://zhaohaiyang.blog.51cto.com/2056753/756083

23.頂端粘滯效果

viewForHeaderInSection可以自訂每段的標題視圖。

24.頂部下拉重新整理,底部上拉取更多示意效果

      下拉重新整理視圖被添加到了tableview中的scrollView的下面,上拉視圖是作為了tableview的footview。

      它們都是根據scrollView的拖動響應,展示不同的狀態,以示意重新整理或取更多操作。

25.UITableView手指橫向滑動偵聽

http://www.1000phone.net/thread-6746-1-1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.