若是能準確知道數組容量的時候,不要用array直接實例化可變數組,[NSMutableArray arrayWihCapacity:50]; < 可優化很多。ios
。若是有須要跟蹤NSArray中的內容,須要重寫descriptionWithLocal 方法。首先新建一個NSArray 的Category,在新建出來的.m文件中加入下列代碼數組
- (NSString *)descriptionWithLocal:(id)local 緩存
{app
NSMutableString *string = [NSMutableString string];ide
[string appendString:@"("];優化
for (id obj in self) {編碼
[string appendFormat:@"\n\t\"%@\",", obj];spa
}代理
[string appendString:@"\n)"];orm
return string;
}
如此一來,用NSLog顯示NSArry中的數據再也不是utf-8編碼,顯示爲原來數據。
UITableView
須要一個數據源來顯示數據
1.需加入數據源協議 UITableViewDataSource ; <
2. 設置表格的組數 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView ; <
3.設置每一個分組中的行數 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ; <
4.設置每一個分組中顯示的數據內容 - (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPaht:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIndentifier:nil];
indexPath.section 即爲分組名 indexPath.row 即行名
} cell便是返回的分組中的數據。
[cell.textLabel setText: ]; <
[cell.imageView setImage: ]; <
[cell.detailTextLabel setText: ]; <
每一個ViewCell中都有一個輔助指示視圖 accessoryType 默認是UITableViewCellAccessoryNone 即不顯示輔助指示視圖 其餘值以下
經過 cell setAccessoryType:UITableViewCellAccessoryNone ; < 來設置
5.設置分組的標題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSIntger)secton ; <
6.設置頁腳的標題, 分組底部的子標題 - (NSString *)tableView:(UITableVIew *)tableView titleForFooterInSection:(NSInteger)section ; <
7.字典返回全部keys的方法 NSArray *items = [self.cities allKeys]; < 裏面數據出來的順序是亂的
8.將UITableView中的分組分開(即設置style,但style只能在初始化的時候設定)
UITableView *tableView = [[UITableView alloc] initWithFrame: style:(UITableViewStyle)] ; <
9.代理方法: 選中某一行所進行的操做 - (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndex *)indexPaht ; <
右側索引方法 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView ; <
設置行高 - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath ; <
10.刷新某一行的數據 [tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft]; <
11.刷新所有數據 [tableView reloadData];
12.更新某一行數據 [_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft];
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UITableView的優化:(條件爲在storyBoard中的tableView中添加一個tableViewCell, 並設置identifier爲myCell)否則會報錯!
static NSString *cellIdentifier = @"myCell"; //設置統一的標示符 static 能夠保證表格標識符永遠只有一個
UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:cellIndentifier]; //首先在緩存池中找名爲''myCell''的單元格對象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)#> forIndexPath:<#(NSIndexPath *)#>] //自定義單元格時使用
if (cell = nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellstyleDefault reuseIdentifier:cellIdentifier]; //若是沒找到,實例化一個新的cell
} ios中,當頁面內的tableView能容納10行,當向下滑動時,第一行的數據地址進入緩存池,新刷出來的第十一行將從緩存池取出原第一行的地址用於存儲第十一行數據
若是使用了storyBoard,而且,在storyBoard中指定了"單元格"的可重用標識符,那麼
dequeueReusableCellWithIdentifier
dequeueReusableCellWithIdentifier
第一種狀況:
若是在viewDidLoad註冊了nib文件 / 若是在viewDidLoad註冊了自定義單元格的類,而且指定了''單元格''的可重用標識符,那麼
dequeueReusableCellWithIdentifier
dequeueReusableCellWithIdentifier:forIndexPath 兩個方法等效
第二種狀況:
若是沒有在viewDidLoad註冊nib文件 / 沒有在viewDidLoad註冊自定義單元格的類,那麼,只能使用
dequeueReusableCellWithIdentifier 而且須要判斷cell沒有被實例化,並作相應的處理
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
經過拖動控件的方式來建立TableViewControl時,需注意設置tableViewCell的identifier!
用segue進行View的切換 在prepareForSegue方法中可找到全部的segue,但不一樣的segue的標識符不同
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CitiesViewController *controller = segue.destinationViewController ;
[controller setCities:cities]; 在CitiesViewController.h文件中定義了cities這一屬性,並用這一屬性構造了視圖,此處只需用cities設置屬性便可
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
[tableView registerClass:[chatCell class] forCellReuseIdentifier:@"myCell"] 爲表格註冊自定義單元格的類,並指定單元格的可重用"標識符"
----------------
1.在storyBoard中直接自定義單元格會註冊單元格原型
2.用xib方式自定義單元格須要註冊NIB
UINib *nib = [UINib nibWithNibName:@"bookCell" bundle:[NSBundle mainBundle]];
[tableView registerNib:nib forCellReuseIdentifier:@"cell"];
3.用代碼方式自定義單元格須要註冊類
[tableView registerClass:[MyCell class] forCellReuseIdentifier:CellIdentifier];
如下方法的目的就是要求必須註冊自定義單元格類
[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
細節決定成敗!