无法在 UITableView 中显示文本

发布于 2025-01-02 20:46:44 字数 1087 浏览 0 评论 0 原文

我有一个基于视图的应用程序,只做一件事:在 TableView 中显示一个数组,但它不起作用,我不知道为什么。这是代码。

.h

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    NSArray *array;
    IBOutlet UITableView *table;
}

.m

-(void)viewDidLoad{
[super viewDidLoad];
    array = [array initWithObjects:@"iPad", @"iTV", nil];

}

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

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

- (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] autorelease];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    return cell;
}

有什么想法为什么 TableView 不显示文本吗?谢谢你们!

I have a view-based app that just does one thing : show a array in a TableView, but it doesn't work and I don't know why. Here's the code.

.h

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    NSArray *array;
    IBOutlet UITableView *table;
}

.m

-(void)viewDidLoad{
[super viewDidLoad];
    array = [array initWithObjects:@"iPad", @"iTV", nil];

}

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

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

- (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] autorelease];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];

    return cell;
}

Any ideas why the TableView doesn't show the text? Thank you people!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事随风而去 2025-01-09 20:46:44
array = [array initWithObjects:@"iPad", @"iTV", nil];

还应该

array = [NSArray initWithObjects:@"iPad", @"iTV", nil];

确保您像大麦提到的那样调用 arrayInit

编辑:
文斯正确地指出它应该是

[[NSArray alloc] initWithObjects:@"iPad", @"iTV", nil];

[NSArray arrayWithObjects:@"iPad",@"iTV",nil];

取决于您是否想保留或自动释放它

array = [array initWithObjects:@"iPad", @"iTV", nil];

Should be

array = [NSArray initWithObjects:@"iPad", @"iTV", nil];

also make sure you call arrayInit as barley mentioned

edit:
Vince is right in pointing out it should either be

[[NSArray alloc] initWithObjects:@"iPad", @"iTV", nil];

or

[NSArray arrayWithObjects:@"iPad",@"iTV",nil];

depending on if you want to retain or have it auto released

铁轨上的流浪者 2025-01-09 20:46:44

我假设您在 ARC NSIndexPath 下运行

没有“行”属性尝试

NSInteger row = [indexPath indexAtPosition:1];
cell.textLabel.text = [array objectAtIndex:row];

Ill assume you are running under ARC

NSIndexPath doesn't have a "row" property try

NSInteger row = [indexPath indexAtPosition:1];
cell.textLabel.text = [array objectAtIndex:row];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文