NSMutableArray
을 사용하여 채울 UITableView
이 있습니다. 이 tableview
은 스크롤 할 때 업데이트되며 NSMutableArray
에 더 많은 데이터를 추가하여 업데이트됩니다. 내가 직면하고있는 문제는이 페이지에서 다른 페이지로 이동 한 다음 다시 돌아올 때마다 tableview
이 배열의 초기 크기로 설정된다는 것입니다. 얼마나 많은 업데이트를 수행하든 (즉, 매번 10 개의 객체를로드하면, 테이블 크기는 배열 크기가 30 인 경우에도 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, cellDataVIar (nonatomic, retain) 이외의 속성을 설정해야합니까? –