我有一个UITableView
我填充使用NSMutableArray
。此向下滚动时会更新这个tableview
,这是通过将更多数据添加到NSMutableArray
而实现的。我面临的问题是,每次我从这个页面导航到另一个页面,然后再返回,tableview
被设置为数组的初始大小,无论我做了多少更新(意思是说,如果我每次加载10个对象,即使数组大小为30,tableview大小也会恢复为10,注意:数组大小决不会仅更改表内容大小)。我开始相信这与NSMutableArray
的属性有关。代码的要点是这样的:NSMutableArray属性来填充UITableView
@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
我希望不是太多。请告诉我哪里可能会出错。
使数组“静态”是一个糟糕的出路,几乎总是错误的答案。该数组确实需要被定义为一个继续存在的类中的ivar。 – zaph
就像我说过的,数组的大小保持不变,只有UITableView的大小发生变化。我用NSLog语句来确认这一点。 –
@Zaph,我需要为cellData ivar设置一个属性(nonatomic,retain)吗? –