代码:
在.h中:
NSMutableArray *contentArray;
我正在声明我的数组。
<小时/>以 .m 为单位
- (void)viewDidLoad
{
[super viewDidLoad];
contentArray = [[NSMutableArray alloc] initWithObjects:@"view", @"browse", @"create", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
我正在将其设置为我的 View 已加载。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [contentArray 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];
}
// Configure the cell...
[[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];
return cell;
}
没有。但如果我这样做 "[[cell textLabel] setText:@"Hello World."];"相反,它工作得很好。
最佳答案
表中至少有 3 个空行吗?如果是,那么只需更改您的代码
NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
如果您甚至没有获得空字符串,请在 .h 文件中创建数组的属性。在 .m 中合成它(也在 delloc 函数中释放它),最后在紧随其后的 viewDidLoad 中
NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:@"view", @"browse", @"create", nil];
self.contentArray=tempArray;
然后编写以下代码来获取单元格标题
NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
关于iphone - UITableView 不显示数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106633/