簡體   English   中英

將數據加載到uiviewcontroller中的uitableview中

[英]Loading data into a uitableview in a uiviewcontroller

我正在嘗試從通過解析json制成的數組中加載數據...我肯定在數組中包含json數據,但是甚至沒有調用表視圖方法...我將表視圖嵌入常規的uiviewcontroller,它會編譯,並且表格中的所有內容都不會加載任何數據。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [transactions count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"any cell"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"any cell"] autorelease];
    }

    NSLog(@"got here");
    cell.textLabel.text = [names objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [prices objectAtIndex:indexPath.row];
    return cell;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    names = [[NSMutableArray alloc] init];
    prices = [[NSMutableArray alloc] init];
    [self requestTransactionData];
}

這是我的.h文件...

@interface Controller1 : UIViewController
    <UITableViewDelegate, UITableViewDataSource>
{
    UIActivityIndicatorView *loadingIndicator;

    IBOutlet UITableView *myTableView;
    IBOutlet UILabel *totalLabel;

    NSMutableArray *names;
    NSArray *transactions;
    NSMutableArray *prices;
}

@property(nonatomic, retain) IBOutlet UIActivityIndicatorView *loadingIndicator;
@property(nonatomic, retain) IBOutlet UILabel *totalLabel;

@property(nonatomic, retain) UITableView *myTableView;

我不能為此項目使用ARC。 我也已經將它正確地連接到了界面生成器中……這似乎很容易,但是由於某種原因我無法弄清楚。 有什么建議么?

謝謝

在將價格填入requestTransactionData之后,調用sef.tableView reloadData

  1. 我看不到您如何知道未調用數據源方法。 您沒有記錄所有這些。 如果在填充transactions之前將numberOfRowsInSection調用為零或為nil,則它將返回零,並以此結束。

  2. 嘗試使用延遲的性能調用reloadData以便填充transactions 之后發生。 表格視圖無法神奇地知道何時准備好數據。 你必須告訴它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM