標籤:des style blog os 使用 io strong for ar
一、本次小例子:
二、代碼如下:
#import <Foundation/Foundation.h>@interface Product : NSObject//標題@property (strong, nonatomic) NSString * title;//描述@property (strong, nonatomic) NSString * desc;//圖片@property (strong, nonatomic) NSString * imageName;@end
#import <UIKit/UIKit.h>@interface ViewController : UITableViewController<UIAlertViewDelegate>@end
#import "ViewController.h"#import "Product.h"@interface ViewController ()@property (strong, nonatomic) NSMutableArray * arrProducts;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; //0. 數組初始化 //執行個體化商品列表數組,如果能夠準確知道數組容量的時候,不要用array直接執行個體化可變數組 self.arrProducts = [NSMutableArray arrayWithCapacity:50]; //1. 建立Model for (int i=0; i<50; i++) { Product * pro = [[Product alloc] init]; pro.title = [NSString stringWithFormat:@"商品%i", i+1]; pro.desc = @"描述商品資訊"; pro.imageName = [NSString stringWithFormat:@"00%i", arc4random_uniform(9)+1]; //2. 添加到數組 [self.arrProducts addObject:pro]; } }#pragma mark - tableView dataSource#pragma mark 分組數量#pragma mark 每組行數-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{ return self.arrProducts.count;}#pragma mark 每行內容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; Product * pro = self.arrProducts[indexPath.row]; //1. cell標題 cell.textLabel.text = pro.title; //2. cell表徵圖 cell.imageView.image = [UIImage imageNamed:pro.imageName]; //3. cell詳細資料 cell.detailTextLabel.text = pro.desc; //4. cell右側表徵圖 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //箭頭 return cell;}#pragma mark TableView Delegate//cell 高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}#pragma mark 點擊某一行-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ Product * pro = self.arrProducts[indexPath.row]; UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"標題" message:@"我在學習TableView" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; [alert setAlertViewStyle:UIAlertViewStylePlainTextInput]; //設定樣式 [[alert textFieldAtIndex:0] setText:pro.title]; //賦值 [alert show];}#pragma mark - UIAlertView Delegate- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //NSLog(@"%i", buttonIndex); if (!buttonIndex) //取消 return; //0. 獲得修改 NSString * stringTitle = [alertView textFieldAtIndex:0].text; //1. 修改model NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow]; Product * pro = self.arrProducts[indexPath.row]; pro.title = stringTitle; //2. 重新整理TableView //2.1 重新整理全部資料,當數量龐大時,不合適。(無動畫效果) //[self.tableView reloadData]; //重新整理局部(有動畫效果) [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade]; }@end
三、 知識點記錄
1. 引入 MVC的模型(Model),即:Product 。
1.1 聲明了幾個屬性,為了處理對模型資料的修改。
1.2 初始化模型,並添加到數組中。
2. 設定Cell高度方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3. 使用UIAlertView 及 代理。
4. 重新整理tableView。
4.1 局部重新整理 (有動畫效果)
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationFade];
4.2 全部重新整理(沒有動畫效果)
[self.tableView reloadData];
5. 補充一個MVC架構的圖片。易於理解。
四、 問題記錄
1. 像QQ, 等, 發出的資訊, 自動適配Cell高度, 怎麼做到的?
2. UIAlertView 除了枚舉的那幾個樣式, 怎麼自訂UI ?
五、隨筆
1. IOS app 有好多都是 UITableView 列表的效果。
2. 儘可能是理解基本屬性,方法。 等熟了, 嘗試 抽tableView。 加油!
第4課、UITableView專題(三)