我正在为iOS 9.0编写应用程序,但遇到一个错误,我无法解决以挽救我的生命!简而言之,当我在AlertView的NSMutableArray愿望清单中添加一项时,愿望清单包含其所有元素,包括新添加的元素。但是,当我调用[self.tableView reloadData]时,有时新值的所有成员数据都设置为nil。需要明确的是,... reloadData ...中的wishlist确实具有n + 1个元素,但最后一个是SOMETIMES nil。
例如,添加一个带有food_item参数“test”或“test 1”的对象就可以了。但是输入“test 2 2 a”会导致数据在重新加载时设置为nil。
我试图通过使愿望清单成为.h文件中的强非原子属性来解决此问题,但这没有用。我可以确认数据库方面的工作状况很好,因为如果在重新加载表格视图数据之前重置心愿单中的所有值,那么我所有的元素都在那里(但这会造成一些麻烦)。
有任何想法吗?
#import "WishlistViewController.h"
#import "usersDAO.h"
#import "wishlistitemDAO.h"
#import "wishlistDAO.h"
#import "TabBarController.h"
#import "MGSwipeButton.h"
#import "MGSwipeTableCell.h"
@interface WishlistViewController ()
@end
@implementation WishlistViewController{
NSMutableArray *wishlist;
}
@synthesize username, house_name, house_id, navBar, admin;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TabBarController *tabBar = (TabBarController *)self.tabBarController;
self.username = tabBar.username;
self.house_id = tabBar.house_id;
self.house_name = tabBar.house_name;
self.admin = tabBar.admin;
wishlistitemDAO *wliDAO = [wishlistitemDAO new];
wishlistDAO *wlDAO = [wishlistDAO new];
self->wishlist = [[NSMutableArray alloc] init];
wlDAO.id = [wlDAO getCurrentWishlistFromHouseId:house_id];
self->wishlist = [wliDAO getWishlistItemsWithWishlistId:wlDAO.id];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [wishlist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
wishlistitemDAO *wliDAO = [wishlistitemDAO new];
static NSString * reuseIdentifier = @"programmaticCell";
MGSwipeTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
cell.textLabel.text = [wliDAO getFoodItem:[wishlist objectAtIndex:indexPath.row]];
//configure left buttons
cell.leftButtons = @[[MGSwipeButton
buttonWithTitle:[NSString stringWithFormat:@"%@",[wliDAO getQuantity:[wishlist objectAtIndex:indexPath.row]]]
backgroundColor:[UIColor greenColor] setClickable:false],
[MGSwipeButton
buttonWithTitle:@"" icon:[UIImage imageNamed:@"thumbs-up.png"] backgroundColor:[UIColor cyanColor]],
[MGSwipeButton
buttonWithTitle:@"" icon:[UIImage imageNamed:@"thumbs-down.png"] backgroundColor:[UIColor redColor]]
];
cell.leftSwipeSettings.transition = MGSwipeTransition3D;
if([admin isEqualToNumber:[NSNumber numberWithInt:1]]){
//configure right buttons
cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@""
icon:[UIImage imageNamed:@"trash-can.png"] backgroundColor:[UIColor redColor]
callback:^BOOL(MGSwipeTableCell *sender) {
wishlistitemDAO *wliDAO = [wishlistitemDAO new];
[wliDAO removeItemFromWishlist:[wliDAO getId:[wishlist objectAtIndex:indexPath.row]]];
[self->wishlist removeObjectAtIndex:indexPath.row];
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
return 1;
}],
[MGSwipeButton buttonWithTitle:@""
icon:[UIImage imageNamed:@"shopping-cart-add.png"] backgroundColor:[UIColor cyanColor]
]
];
cell.rightSwipeSettings.transition = MGSwipeTransition3D;
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"wishlistLogoutSegue"]){
}
}
- (IBAction)addItem:(id)sender {
//alert view
UIAlertView *message = [[UIAlertView alloc]
initWithTitle: @"New Item"
message: @""
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: @"Add",nil];
message.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[[message textFieldAtIndex:1] setSecureTextEntry:false];
[[message textFieldAtIndex:0] setPlaceholder:@"Item Name"];
[[message textFieldAtIndex:1] setPlaceholder:@"Quantity"];
[message show];
}
//which button was clicked by the user
-(void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch(buttonIndex) {
case 0:{
break;
}
case 1:{
UITextField *food_item = [alertView textFieldAtIndex: 0];
UITextField *quantity = [alertView textFieldAtIndex:1];
wishlistitemDAO *wliDAO = [wishlistitemDAO new];
wishlistDAO *wlDAO = [wishlistDAO new];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *n = [f numberFromString:quantity.text];
[wliDAO addWishlistItem:food_item.text withQuantity:n toWishlistId:[wlDAO getCurrentWishlistFromHouseId:house_id]];
wlDAO.id = [wlDAO getCurrentWishlistFromHouseId:house_id];
[wishlist addObject:[[wliDAO getWishlistItemsWithWishlistId:wlDAO.id] lastObject]];
[self.tableView reloadData];
break;
}
default:
break;
}
}
@end
最佳答案
首先,使用wishlist
访问类似于self->wishlist
的成员,删除self->
很奇怪,因为wishlist
本身在该类的方法中引用了它。它表现得很强。但是,由于您已经在使用username
等属性,因此最好将wishlist
设置为属性并保持一致:
@interface WishlistViewController ()
@property (nonatomic, strong) NSMutableArray *wishlist
@end
@implementation WishlistViewController
// @synthesize line is redundant since 2012
- (void)viewDidLoad {
...
(在您的代码中,我在下面引用您的代码,我假设您已完成此操作,而我将
self->
替换为self.
,因为前者会伤害我的大脑来打字甚至看。)现在的答案是,Carl在提到分配
viewDidLoad
的wishlist
行时确实是正确的。很明显,您可能会有一些误解,因为您要创建一个数组,然后在两行后替换它:self.wishlist = [[NSMutableArray alloc] init]; // CREATE
wlDAO.id = [wlDAO getCurrentWishlistFromHouseId:house_id];
self.wishlist = [wliDAO getWishlistItemsWithWishlistId:wlDAO.id]; // STOMP
您可能要做的就是用
wishlistDAO
的结果填充数组,例如:self.wishlist = [[NSMutableArray alloc] init];
wlDAO.id = [wlDAO getCurrentWishlistFromHouseId:house_id];
[self.wishlist addObjectsFromArray:[wliDAO getWishlistItemsWithWishlistId:wlDAO.id]];
但是您也可以删除第一条
wishlist =
行,并像Carl所建议的那样对方法的结果进行可变复制,但是像self.wishlist = [wliDAO getWishlistItemsWithWishlistId:wlDAO.id].mutableCopy;
那样进行操作则比较麻烦可能发生的是
wishlistDAO
返回其内部可变数组,该数组可能在以后使用并更改,而这就是wishlist
设置的即。设置为参考,即指向wishlistDAO
自己的NSMutableArray
对象的指针值。不好,这是上面建议的更改所解决的问题。然后在您的调用中再次使用相同的方法:[wishlist addObject:[[wliDAO getWishlistItemsWithWishlistId:wlDAO.id] lastObject]];
我猜想它是从擦除其数组开始的,因为它是同一对象,所以会擦除您的数组。也许它也会在过程中释放它并创建一个新的,而
wishlist
保留为现在为空的数组的唯一保留。为了安全起见,请注意最佳做法:
getWishlistItems
应该返回数组的新副本,而不是对某些内部属性的引用,或者以其他方式返回后可能会更改的方式。该方法的结果应声明为不可更改的NSArray
。 addObjectsFromArray
,或将wishlist
分配给该方法的mutableCopy
。 一个人单独做应该可以解决您的问题。犯下这两个错误肯定是原因的组合。