【问题标题】:Xcode Compile Error "expected method to read array element not found on object of type NSArray" [duplicate]Xcode编译错误“在NSArray类型的对象上找不到读取数组元素的预期方法”[重复]
【发布时间】:2013-09-14 08:25:40
【问题描述】:

我是 xcode 新手,我尝试使用 UITableView 来显示数组中的内容。

我尝试在 Feed 中放入一些数组,并尝试在表格中显示它们。

但在这种情况下会提示错误

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

在 cell.textLabel.text = self.imageTitleArray[indexPath.row];

它表示读取 NSArray 类型对象上未找到的数组元素的预期方法

我很困惑,为什么它不会读取数组,队长明显请帮助

这是我的 H 文件

#import <UIKit/UIKit.h>

@interface FeedTableViewController : UITableViewController
@property (strong, nonatomic) NSArray *imageTitleArray;
@end

这是我的 M 文件

#import "FeedTableViewController.h"

@interface FeedTableViewController ()

@end

@implementation FeedTableViewController



- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
    self.title = @"Feed";
    self.imageTitleArray = @[@"Image 1",@"Image 2",@"Image 3", @"Image 4",@"Image 5"];
}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  return self.imageTitleArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //  static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if(cell==nil){
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
  cell.textLabel.text = self.imageTitleArray[indexPath.row];    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end

【问题讨论】:

  • 可能你在模拟器中运行它,iOS 版本低于 6.0。
  • 是的,我的模拟器还在 5.1,不兼容吗?
  • 是的,它与低于 6.0 的版本不兼容

标签: ios objective-c uitableview nsarray


【解决方案1】:

现在尝试这样做,这将在短期内解决您的问题:

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

The updated syntax that you're trying to use (which is correct) came in with Xcode 4.5

【讨论】:

    【解决方案2】:

    cell.textLabel.text = self.imageTitleArray[indexPath.row];

    这就是问题——应该是

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

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多