2.3 UItableView-自定義非等高 表格刷新

自定義非等高

// 設(shè)置label每一行文字的最大寬度
    // 為了保證計算出來的數(shù)值 跟 真正顯示出來的效果 一致
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

編輯

  • 進入編輯模式
  // 進入編輯模式
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];

彈框添加

0605-1.png
    - (IBAction)add {
    // 創(chuàng)建彈框控制器
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請輸入團購信息" message:nil preferredStyle:UIAlertControllerStyleAlert];

    // 添加按鈕
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // 創(chuàng)建模型
        XMGDeal *deal = [[XMGDeal alloc] init];
        deal.title = [alert.textFields[0] text];
        deal.price = [alert.textFields[1] text];
        [self.deals insertObject:deal atIndex:0];

        // 刷新數(shù)據(jù)
        [self.tableView reloadData];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"不知道" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"點擊了不知道按鈕");
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"不知道2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"點擊了不知道2按鈕");
    }]];

    // 添加文本輸入框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"請輸入團購名字";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"請輸入團購價格";
    }];

    // 顯示控制器
    [self presentViewController:alert animated:YES completion:nil];
}

UITableViewDelegate

  • 只要實現(xiàn)這個方法,左劃cell出現(xiàn)刪除按鈕的功能就有了
/**
 * 只要實現(xiàn)這個方法,左劃cell出現(xiàn)刪除按鈕的功能就有了
 * 用戶提交了添加(點擊了添加按鈕)\刪除(點擊了刪除按鈕)操作時會調(diào)用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {  // 點擊了“刪除”
        // 刪除模型
        [self.deals removeObjectAtIndex:indexPath.row];

        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 點擊了+
        NSLog(@"+++++ %zd", indexPath.row);
    }
}
  • 這個方法決定了編輯模式時,每一行的編輯類型:insert(+按鈕)、delete(-按鈕)
    /**
 * 這個方法決定了編輯模式時,每一行的編輯類型:insert(+按鈕)、delete(-按鈕)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}

數(shù)據(jù)刷新方法

  • 重新刷新屏幕上的所有cell
[self.tableView reloadData];
  • 刷新特定行的cell
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 插入特定行數(shù)的cell
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];
  • 刪除特定行數(shù)的cell
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];

數(shù)據(jù)刷新的原則

  • 通過修改模型數(shù)據(jù),來修改tableView的展示
    • 先修改模型數(shù)據(jù)
    • 再調(diào)用數(shù)據(jù)刷新方法
  • 不要直接修改cell上面子控件的屬性
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容