UITableView刪除移動多選其他基本操作總結

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"刪除");
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        // 刪除 cell
        // 先對數據源(提供數據的數組)進行刪除
        
        [self.array removeObjectAtIndex:indexPath.row];
        
        // 第一個參數:要刪除的cell的indexPath組成的數組
        // 第二個參數:刪除時的動畫
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

    if (UITableViewCellEditingStyleInsert == editingStyle) {
        // 編輯數據源
        [self.array insertObject:@"aa" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

}

// tableView的移動
// 1.設置cell是否可以移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// tableView移動完成調用方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // 先取除要移動的 對象(根據sourceIndexPath)
    NSString *tempName = [[self.array objectAtIndex:sourceIndexPath.row] copy];
    [self.array removeObjectAtIndex:sourceIndexPath.row];
    // 將取出的元素(tempName)添加到目的地 (destinationIndexPath)
    [self.array insertObject:tempName atIndex:destinationIndexPath.row];

    [tempName release];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // 系統的編輯按鈕的觸發方法
    [super setEditing:editing animated:animated];
    
    // 通過系統的編輯狀態的改變 來改變tableView的狀態
    [_tableView setEditing:editing animated:animated];
}


// 給每個區起一個標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (0 == section) {
        return @"456456";
    }
    return @"123123";
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    label.backgroundColor = [UIColor greenColor];
    label.text = @"aaaa";
    return [label autorelease];
}


// 定義右側的索引信息
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 創建一個重用標識字符串
    static NSString *str = @"cellReuse";
    // [2]
    // 利用tableView內部的重用池(NSSet) 產生cell
    // 1.從重用池中取一個cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    <p style="margin-top: 0px; margin-bottom: 0px; font-family: 'Heiti SC Light';"><span style="font-family: Menlo;">    </span><span style="font-family: Menlo;">// </span>如果同一個<span style="font-family: Menlo;">tableView</span>裏面需要使用多個不同佈局的<span style="font-family: Menlo;">cell,</span>在這個方法中需要創建多個不同的<span style="font-family: Menlo;"> </span>重用標識<span style="font-family: Menlo;">, </span>用於區分不同的<span style="font-family: Menlo;">cell</span></p>
    // 2.判斷取出的cell是否是空(nil)
    if (nil == cell) {
        // 如果重用池中沒有現成的cell,就自己創建一個cell
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        NSLog(@"創建cell");
    }
    // 3.給cell的label賦值 獲取當前的位置(indexPath)對應的數組 元素
    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"a.JPG"];
    // 設定cell的輔助視圖
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    // 4.將獲得的cell返回
    return cell;
}

新添加一個知識:

// 系統提供了一個方法 在創建tableView的時候,允許tableView註冊一個cell

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"str"]; 然後刪掉判斷重用池中的判斷cell == nil 這段代碼

缺點:無法確定cell的樣式,一般只是做單方面的cell的處理


    // 設定tableView的樣子

    // 分割線的樣式
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    // 分割線的顏色
    [tableView setSeparatorColor:[UIColor blackColor]];
    // cell的高度(每行的高度)
    [tableView setRowHeight:100];

// 開啓viewController的編輯狀態
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 插入|刪除 既爲多選
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 可以根據indexPath判斷
    return YES;
}

// viewController的點擊 編輯按鈕 的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    NSLog(@"%d", editing);
    [super setEditing:editing animated:animated];
    
    // 開啓tableView的編輯狀態
    [self.tableView setEditing:editing animated:animated];
}




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