1

UITableView使用してデータを入力しNSMutableArrayます。これtableviewは、下にスクロールすると更新されます。これは、にデータを追加することで実現されますNSMutableArray。私が直面している問題は、このページから別のページに移動してから再び戻るtableviewたびに、更新の数に関係なく、配列の初期サイズに設定されることです(つまり、毎回10個のオブジェクトをロードすると、配列サイズが30の場合でも、テーブルビューサイズは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

それが多すぎないことを願っています。どこが悪いのか教えてください。

4

5 に答える 5

1

配列を共有クラス/シングルトンクラスに格納します。独自の共有クラスを作成することも、を使用することもできますappDelegate

  • NSMutableArrayappDelegateでプロパティを宣言します。@property (nonatomic, strong) NSMutableArray *cellData; そしてそれを合成します。

  • cellData配列の代わりにこの配列を使用してください。 にAppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];置き換えますself.cellDataappDelegate.cellData

于 2013-01-14T12:01:28.333 に答える
1

配列を共有クラス/シングルトンクラスに格納します。

このページをリロードすると、viewDidLoadが呼び出され、配列がalloc + initで再度実行され、デフォルト値に設定されます。

于 2013-01-14T11:03:40.647 に答える
0

ビューから削除した場合はself.cellData = [[NSMutableArray alloc] init];、JITアプローチを使用して独自のゲッターをロードして作成しました。

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

cellDataの内容は、FlowControllerの割り当てが解除されるまで保持されます。

于 2013-01-14T11:10:10.613 に答える
0

配列はviewDidLoadで再作成されています。割り当てる前に、配列がすでに存在するかどうかを確認できます。

if (!self.cellData) {
    self.cellData = [[NSMutableArray alloc] init];
    [self getData];
}
于 2013-01-14T11:30:10.803 に答える
0

コードのどこが悪いのかわからなかったので、一時的に修正しました。私が言ったように、NSMutableArray cellDataは変更されていませんが、変更されているのはUITableView自体だけです(これはviewDidDisappearメソッドで見つかりました)。だから私はこれを補うためにこれをしました

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

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

}

これにより、flowTableが最後に選択されたセルに戻ります。エレガントではありませんが、機能します。アイデアを投稿し続けてください。私は本当にこれの底に行きたいです。

于 2013-01-14T13:10:39.380 に答える