-
我在UITableView(六)數據更新(增、刪、改)中新增方法的數據寫的是固定數據,不能自己添加。所以在這篇會用UIAlertController把新增改為自己輸入數據的方式。
- 小tips
- 蘋果在iOS8之后用
UIAlertController
替代了UIActionSheet
和UIAlertView
-
- (IBAction)Add:(UIButton *)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"新增" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入title";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入價格";
}];
// 按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
// 確定按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
WXData *data = [[WXData alloc]init];
data.title = alert.textFields[0].text;
data.price = alert.textFields[1].text;
// data.buyCount = @"22";
data.icon = @"2c97690e72365e38e3e2a95b934b8dd2";
// 在數據源的第一個位置插入數據
[self.list insertObject:data atIndex:0];
// 刷新插入行,有動畫效果
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
}]];
// 顯示控制器
[self presentViewController:alert animated:YES completion:nil];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"111" message:@"2222" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
// [alert show];
// 刷新全部數據,但是沒有動畫效果
// self.tableView reloadData];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。