简体   繁体   English

无法在UITableView中显示文本

[英]Can't show text in the UITableView

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. 我有一个基于视图的应用程序,它只做一件事:在TableView中显示一个数组,但是它不起作用,我也不知道为什么。 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? 有什么想法为什么TableView不显示文本? Thank you people! 谢谢大家!

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

Should be 应该

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

also make sure you call arrayInit as barley mentioned 还请确保按大麦提到的那样调用arrayInit

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 取决于您是保留还是自动释放

Ill assume you are running under ARC 假设您在ARC下运行

NSIndexPath doesn't have a "row" property try NSIndexPath没有“行”属性尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM