學習IOS開發的第11天

今天學習了表視圖單元格的編輯。接着之前的項目,我們爲了方便,用UITableViewController的子類BookMarkTableViewController替換掉BookMarkViewController,讓它顯示錶視圖,並且把它添加到一個導航控制器上。
    BookMarkTableViewController *bookMark = [[BookMarkTableViewController alloc] init];
    //bookMark.view.backgroundColor = [UIColor whiteColor];
    UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2];
    bookMark.tabBarItem = tabBarItem2;
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:bookMark];
在BookMarkTableViewController中創建一個全局變量NSMutableArray *ducks,這個是單元格的顯示內容。在初始化方法裏初始化ducks;
       ducks = [NSMutableArray arrayWithArray:@[@"1只鴨子",@"2只鴨子",@"3只鴨子",@"4只鴨子",@"5只鴨子",@"6只鴨子",@"7只鴨子",@"8只鴨子",@"9只鴨子"]];
然後在loadView方法裏創建表視圖,爲導航欄設置編輯按鈕。
-(void)loadView
{
    //創建表視圖
    UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame] style:UITableViewStyleGrouped];
    self.view =table;
   table.dataSource = self;
    table.delegate = self;
    //編輯按鈕
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
接着實現數據源協議裏的方法來設置section的數量,和row的數量。
//返回section的數量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
//返回section中單元格的數量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [ducks count];
}
然後創建單元格,並設置顯示內容。
//返回單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // tableView.tableHeaderView.textLabel.text = @"1";
 // 定義一個靜態標識符
 static NSString *cellIdentifier = @"cell";
 // 檢查是否有空閒的單元格
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 // 創建單元格
 if (cell == nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
 }
 // 給cell內容賦值
 //cell.textLabel.text = [NSString stringWithFormat:@"%d只鴨子",indexPath.row];
 cell.textLabel.text = ducks[indexPath.row];

 //副標題
 cell.detailTextLabel.text = [NSString stringWithFormat:@"%d只鵝",indexPath.row];
 //設置圖片
cell.imageView.image = [UIImage imageNamed:@"apple.jpeg"];
 
 return cell;
}
要讓單元格可以編輯,要實現幾個代理方法。
//是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return NO;
    }
    return YES;
}
可以爲單元格設置編輯的類型。
//設置單元格編輯的類型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==1) {
        //插入
        return UITableViewCellEditingStyleInsert;
    }
    //刪除
    return UITableViewCellEditingStyleDelete;
}
接下來要編寫執行編輯時的操作,具體請看註釋。
//編輯時的操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //從數組中刪除
        [ducks removeObjectAtIndex:indexPath.row];
        //刪除這一列
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        //插入新內容到數組
        [ducks insertObject:@"新的鴨子" atIndex:indexPath.row+1];
        //創建一個新的NSIndexPath
        NSIndexPath *_indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
        //在表視圖中插入新的單元格
        [self.tableView insertRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}
要想讓單元格移動,可以實現如下方法,讓所有可編輯的單元格都能移動。
//是否可移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
執行移動時,就會執行如下操作。
//移動的操作
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    NSString *temp = [ducks objectAtIndex:fromIndexPath.row];
    [ducks removeObject:temp];
    [ducks insertObject:temp atIndex:toIndexPath.row];
}

最後,我們實現了插入、刪除和移動的操作。


運行結果截圖:

1.點擊編輯後的界面


2.實現插入操作


3.實現刪除操作


4.實現移動操作


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章