2

I am new to xcode, and I try to play with UITableView to show content in array.

I try to put some array inside Feed, and try to show them in table.

but the error is hinted at this instances

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

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

it says expected method to read array element not found on object of type NSArray

I am confused, why it wont read the array, captain obvious please help

this is my H files

#import <UIKit/UIKit.h>

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

and this is my M files

#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
4

2 回答 2

1

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

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

您尝试使用的更新语法(正确)随 Xcode 4.5 提供

于 2013-09-14T08:32:08.250 回答
1

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

这就是问题所在——应该是

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

于 2013-09-14T08:34:32.233 回答