自定義非等高
// 設(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上面子控件的屬性