ios - 用于填充 UITableView 的 NSMutableArray 属性

标签 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

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

最佳答案

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

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

关于ios - 用于填充 UITableView 的 NSMutableArray 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317098/

相关文章:

ios - 适用于 iOS 的 AS3 DatePicker Air

ios - Xcode 10 build 10A255 需要清理构建文件夹以进行所有更改

objective-c - 如何在我的 OS X 应用程序中使用颜色窗口?

performance - 带有大 UIImageView 的自定义 UITableViewCell 导致滚动延迟

ios - UITableView 部分删除问题

iphone - 如何将 WebDAV 合并到我的 iPhone 应用程序中?

ios - SpriteKit - CPU/Memory leak on segue back to main screen

ios - 自定义 UITableViewCell 不更新

ios - 在iOS的UITableView中获取选中标记行

ios - 如何检测指纹传感器不可用的设备