您好,登錄后才能下訂單哦!
#import <UIKit/UIKit.h> #import "SecondLevelViewController.h" @interface CheckListController : SecondLevelViewController @property (nonatomic,strong) NSArray *list; @property (nonatomic,strong) NSIndexPath *lastIndexPath;// @end
-(void)viewDidLoad { NSArray *array = [[NSArray alloc] initWithObjects:@"Who has",@"BUB gruop",@"Who Pudding",@"Scodey Snack",@"EverLasting",@"Green Eggs annd",@"Soy Lent",@"Hard Tack",@"Lembas",@"Roast Breaf",@"Lembas Bread",@"Roast Beast",@"Bancmangr", nil]; self.list = array; [super viewDidLoad]; }
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentfier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier]; } // 從單元視圖中提取行 NSUInteger row = [indexPath row]; // 從當(dāng)前選項(xiàng)提取行 NSUInteger oldRow = [self.lastIndexPath row]; // 從數(shù)組中獲取一行值,將他分給單元格的標(biāo)題 cell.textLabel.text = [self.list objectAtIndex:row]; // 判斷兩個(gè)行是否是一行,如果是則cell的accessory屬性為UITableViewCellAccessoryCheckmark,否則設(shè)置為UITableViewCellAccessoryNone,還要確保lastIndexPath不為空 cell.accessoryType =(row == oldRow && self.lastIndexPath !=nil)? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone; return cell; }
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 從單元視圖中提取行 int newRow = [indexPath row]; // 如果獲取上次選中行不為空,則提取上次選中的行 int oldRow = (self.lastIndexPath != nil)?[self.lastIndexPath row] : -1; // 如果從視圖中選取當(dāng)前行和上次在視圖中選取的不一樣 if (newRow != oldRow) { // 獲取剛才選中單元知道標(biāo)記作為拓展圖標(biāo) UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; // 把上次選中的單元的拓展圖標(biāo)設(shè)置為無 UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; // 儲(chǔ)存本次標(biāo)記行索引,下次標(biāo)記時(shí)使用 self.lastIndexPath=indexPath; } // 當(dāng)我們單元表格視圖拓展標(biāo)記改變之后就要把現(xiàn)在標(biāo)記的單元格設(shè)置為選中狀態(tài) [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
/******** Check List *********/ CheckListController *checkListController = [[CheckListController alloc] initWithStyle:UITableViewStylePlain]; checkListController.title = @"Check One"; checkListController.rowImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"]; // 添加控制器對(duì)象到數(shù)組中,顯示在導(dǎo)航控制器一層 [array addObject:checkListController];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *RowControlIdentfier = @"Row ControlIdentfier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RowControlIdentfier]; if (cell == nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RowControlIdentfier]; UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"]; UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; // 由于UIButton的buttonType屬性被聲明為只讀,因此需要用buttonWithType來創(chuàng)建按鈕,不能使用alloc init 來創(chuàng)建,因?yàn)椴荒軐粹o的類型更改為UIButtonTypeCustom,此操作目的是自定義按鈕圖像 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 設(shè)置按鈕大小與圖像匹配 button.frame = CGRectMake(0, 0, buttonUpImage.size.width, buttonUpImage.size.height); // 設(shè)置正常狀態(tài)下按鈕背景圖片 [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; // 設(shè)置按鈕被按下高亮狀態(tài)下的背景圖片 [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted]; [button setTitle:@"Tap" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside]; // 把定義的button分配給單元的附加視圖 cell.accessoryView = button; } NSUInteger row = [indexPath row]; NSString *rowTitle = [self.list objectAtIndex:row]; cell.textLabel.text=rowTitle; return cell; }
-(void)viewDidLoad { if (self.list==nil) { NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"Eeny",@"Meey",@"Miney",@"Miney",@"Moe",@"Toe",@"Error",@"HELAN sdfh",@"AKEB",@"By",@"For", nil]; self.list=array; } // 在導(dǎo)航欄上添加按鈕,并添加一個(gè)觸發(fā)事件 UIBarButtonItem *moveButon = [[UIBarButtonItem alloc]initWithTitle:@"Move" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMove)]; // 設(shè)置按鈕為導(dǎo)航欄上又按鈕 self.navigationItem.rightBarButtonItem = moveButon; [super viewDidLoad]; }
//點(diǎn)擊導(dǎo)航欄右邊按調(diào)用這個(gè)方法,使單元格變成可編輯模式 -(void)toggleMove { [self.tableView setEditing:!self.tableView.editing animated:YES]; if (self.tableView.editing) [self.navigationItem.rightBarButtonItem setTitle:@"Done"]; else [self.navigationItem.rightBarButtonItem setTitle:@"Move"]; }
//通過此方法,表視圖詢問指定行是否可以被刪除,是否可以新行插入 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // 返回值表示不可刪除和新行插入操作 return UITableViewCellEditingStyleNone; }
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 檢索需要移動(dòng)的行 NSUInteger fromRow = [sourceIndexPath row]; // 檢索行新位置 NSUInteger toRow = [destinationIndexPath row]; // 下面的從數(shù)組中移除指定對(duì)象 // 檢索一個(gè)指向要移動(dòng)的對(duì)象的指針,并保留對(duì)象,這樣在數(shù)組中刪除對(duì)象時(shí)候?qū)ο蟛粫?huì)被釋放 id object = [self.list objectAtIndex:fromRow]; [self.list removeObjectAtIndex:fromRow]; // 移除之后插入指定新位置 [self.list insertObject:object atIndex:toRow]; }
-(void)viewDidLoad { if (self.list==nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"computers" ofType:@"plist"]; NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path]; self.list = array; } UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)]; self.navigationItem.rightBarButtonItem = editButton; [super viewDidLoad]; }
#pragma mark - #pragma mark Table View Data Source Methods -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // 獲取當(dāng)前正處于編輯狀態(tài)下的行索引 NSUInteger row =[indexPath row]; // 從屬性列表中刪除該對(duì)象 [self.list removeObjectAtIndex:row]; // 通知tabView表刪除該行 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。