iphone - UITableView 不显示数组中的数据

标签 iphone ios objective-c uitableview

代码:

在.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/

相关文章:

iOS Facebook 集成像 Instagram

ios - 如何使我的 ios app sdk 向后兼容

ios - Apple PushNotification : Need Server Side functionality Guide

iphone - 为什么我在启动 iPhone OS 应用程序时得到 "security policy error"?

ios - 将 UIAlertView 用于 GetReady 标题

ios - 如何在 Xamarin Studio 中使用 iOS Designer 添加高度/宽度约束?

iphone - NSDateFormat 有问题吗?

ios - Parse.com 无法访问指向 PFObject 的指针

ios - UITextField 阻止 UITableViewCell 内的滚动

iphone - 如何将 MapView 设为按钮(iPhone/iPad)?