离开AlertView

时间:2015-11-12 22:11:37

标签: ios objective-c uitableview nsmutablearray

我正在为iOS 9.0编写应用程序而且我遇到了一个我无法解决的错误来拯救我的生命!简而言之,当我在AlertView中向 NSMutableArray wishlist 添加项目时,wishlist包含其所有元素,包括新添加的元素。但是,当我调用 [self.tableView reloadData] 时,有时新值将其所有成员数据设置为nil。需要说明的是, ... reloadData ... 中的 wishlist 确实有n + 1个元素,但最后一个元素是SOMETIMES nil。

例如,添加一个带有food_item参数的对象" test"或"测试1"工作得很好。但是进入"测试2 2 a"导致数据在重新加载时设置为nil。

我试图通过在我的.h文件中使 wishlist 成为一个强大的非原子属性来解决这个问题,但这不起作用。我可以确认数据库方面的工作正常,因为如果我在重新加载tableview数据之前重置了 wishlist 中的所有值,那么我的所有元素都存在(但这会导致一些令人讨厌的延迟)。 / p>

有什么想法吗?

#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

1 个答案:

答案 0 :(得分:1)

首先,使用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的行时,List确实是正确的。很明显你可能有一些误解,因为你正在创建一个数组,然后在2行后更换它:

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作为现在空数组的唯一保留。

为了安全起见最好的做法:

  1. getWishlistItems应该返回一个数组的新副本,而不是对某个内部属性的引用,或者返回它后可能会更改的任何其他方式。该方法的结果应声明为不可变的NSArray
  2. 如上所述,您的视图控制器应该对该方法的结果使用addObjectsFromArray,或者将wishlist分配给mutableCopy
  3. 单独执行任何一项都可以解决您的问题。这是造成这两个错误的组合,这肯定是原因。