用于填充UITableView的NSMutableArray属性

时间:2013-01-14 10:53:21

标签: ios objective-c uitableview nsmutablearray

我使用UITableView填充NSMutableArray。向下滚动时会tableview更新,这是通过向NSMutableArray添加更多数据实现的。我面临的问题是每次我从这个页面导航到另一个页面然后再返回时,tableview被设置为数组的初始大小,无论我做了多少更新(意味着如果我加载十个)每次对象,即使数组大小为30,tableview大小也会恢复为10,注意:数组大小永远不会只改变表的内容大小。我开始相信这与NSMutableArray的属性有关。代码的要点是:

@interface FlowViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    UITableView *flowTable;

    NSMutableArray *cellData;

}
@property(nonatomic, retain) IBOutlet UITableView *flowTable;

@property(nonatomic, retain) NSMutableArray *cellData;

- (void) getData;
- (void) storeData: (NSMutableArray*) arr; 

@end

@implementation FlowViewController

@synthesize cellData;

@synthesize flowTable;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.flowTable.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    [self.flowTable setDataSource:self];
    [self.flowTable setDelegate:self];

    self.cellData = [[NSMutableArray alloc] init];

    [self getData];
}

- (void) storeData:(NSMutableArray *)arr
{
    for(NSDictionary *data in arr)
    {

        CellObject *det = [[CellObject alloc] init];

        // store details

        [self.cellData addObject: det];

    }

    [self.flowTable reloadData];
}

- (void) getData
{

    NSString *url = @"http://example.com/";

    NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                     timeoutInterval:10.0];

[theRequest setHTTPMethod:@"GET"];

flowConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

}


#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.cellData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"FlowCell";

    MyFlowCell *cell = (MyFlowCell *)[self.flowTable dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FlowCell" owner:nil options:nil];
       // cell = [nib objectAtIndex:0];

        for(id currentObject in nib)
        {

        if([currentObject isKindOfClass:[MyFlowCell class]])

        {
            cell = (MyFlowCell *)currentObject;

        break;
    }
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

CellObject *rowItem = [cellData objectAtIndex:indexPath.row];

    // set cell data

}

    return cell;
}


- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.current = indexPath.row;

    [self performSegueWithIdentifier:@"flowToAnotherSegue" sender:nil];

}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"flowToAnotherSegue"])
    {
        NewViewController *iv =
            segue.destinationViewController;

    iv.current = self.current;
        iv.data = self.cellData;

    }

}


#pragma mark -
#pragma NSURLConnection Delegate Methods
- (void) connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Receiving response: %@, status %d", [(NSHTTPURLResponse*)response allHeaderFields],     [(NSHTTPURLResponse*) response statusCode]);
    receivedData = [NSMutableData data];
}

- (void) connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error {
    NSLog(@"Connection Failed: %@", error);   
}

- (void) connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data {

       [receivedData appendData:_data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    // get the new NSMutableArray from receivedData

    [self storeData: newMutableArray];

}


#pragma mark -
#pragma mark Deferred image loading (UIScrollViewDelegate)

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {

    if(Bottom reached) {

       // load more data

        [self getData];

        }

    }
}

@end

我希望这不是太多。请告诉我哪里可能出错了。

5 个答案:

答案 0 :(得分:1)

将数组存储在共享类/单例类中。

每当您重新加载此页面时,都会调用viewDidLoad并且您的数组再次执行alloc + init,并将其设置为默认值。

答案 1 :(得分:1)

将数组存储在共享类/单例类中。您可以创建自己的共享类,也可以使用appDelegate

  • 在appDelegate中声明NSMutableArray属性。 @property (nonatomic, strong) NSMutableArray *cellData; 并合成它。

  • 使用此数组而不是cellData数组。 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];将您的self.cellData替换为appDelegate.cellData

答案 2 :(得分:0)

如果从视图中删除self.cellData = [[NSMutableArray alloc] init];,请加载并使用JIT方法创建自己的getter:

-(NSMutableArray *)cellData
{
    if(!_cellData){
        _cellData = [NSMutableArray array];
    }
    return _cellData;
}

在取消分配FlowController之前,将保留cellData的内容。

答案 3 :(得分:0)

您的数组正在viewDidLoad上重新创建,您可以在分配之前检查它是否已存在。

if (!self.cellData) {
    self.cellData = [[NSMutableArray alloc] init];
    [self getData];
}

答案 4 :(得分:0)

由于我无法找到代码中出现的问题,我已经做了一个临时修复。就像我说的那样,NSMutableArray cellData没有被改变,但唯一改变的是UITableView本身(我在viewDidDisappear方法中发现了这一点)。所以我这样做是为了弥补这个

 - (void) viewDidAppear:(BOOL)animated
{
    [self.flowTable reloadData];

    [self.flowTable scrollToRowAtIndexPath: lastIndex atScrollPosition:0 animated: NO];

}

这会将flowTable返回到最后选择的单元格。它不优雅,但它的工作原理。请继续发表您的想法。我真的想深究这一点。