【问题标题】:UITableView with data from array带有数组数据的 UITableView
【发布时间】:2012-01-18 09:56:24
【问题描述】:

我有一个UIViewController,里面有一个UITableView。我现在想用数组中的数据填充这个表视图。但问题是它什么也没显示。

这是我的实现文件:

#import "MenuViewController.h"

@implementation MenuViewController
@synthesize menuArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.menuArray = [[NSArray alloc] initWithObjects:@"Catalogus",@"Wishlist", nil];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.menuArray = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.menuArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.
    cell.textLabel.text = [self.menuArray objectAtIndex:indexPath.row];

    return cell;
}
@end

这是我的头文件。

#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    NSArray *menuArray;
}
@property (nonatomic,retain) NSArray *menuArray;

@end

在我的 Xib 文件中,我将数据源和委托都连接到文件所有者。

【问题讨论】:

  • 还有什么问题?
  • 是整个头文件吗?如果是这样,您可能需要制作一个 'IBOutlet UITableView *yourTableview;'并在xib中连接

标签: objective-c uitableview xcode4 nsarray


【解决方案1】:

为您的 tableView 设置 Delegate 和 DataSource。 它会起作用的。

【讨论】:

    【解决方案2】:

    在您设置数组的位置,重新加载表格...

    self.menuArray = [[NSArray alloc] initWithObjects:@"Catalogus",@"Wishlist", nil];
    [myTable reloadData];
    

    将“myTable”更改为您的表格名称,如果您使用它,您需要将其作为变量添加到表头并通过 IB 连接。

    【讨论】:

    • 我使用了 @user1155884 提供的相同代码。在笔尖中,我将 tableview 设置为文件所有者数据源和委托。它工作正常。
    【解决方案3】:

    如果您有任何错误,请检查控制台并告诉我们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多