【问题标题】:how to declare array [closed]如何声明数组[关闭]
【发布时间】:2012-02-07 21:21:40
【问题描述】:

我需要别人的帮助来声明以下数组:-

self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];

为:-

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

// Configure the cell.
cell.textLabel.text = @"cell text";
cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]];
return cell;
}

如何在 .h 和 .m 文件中应用它?我对此很陌生。

提前致谢:)

PS。当我申请以下内容时,我会重复“pic1.png”并且没有数组。

{
self = [super init];
if (self) {
// Custom initialization
self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",      @"County", @"City", @"District", nil];
self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];
CGRect frame = self.view.bounds;
frame.size.height -= 100;
self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setScrollEnabled:NO];

[self.view addSubview:self.tableView]; 
}
return self;
}

【问题讨论】:

  • 数组看起来不错,请检查下面的@beryllium 答案以获取下面的 .h 和 .m 内容。但是你的意思是没有细胞被输​​出吗?如果是这样,您是否实现了 UITableView 数据源并具有以下方法: – numberOfSectionsInTableView: – tableView:numberOfRowsInSection: ?

标签: objective-c ios uitableview


【解决方案1】:

.h

@property (nonatomic, retain) NSArray *tablePicture;

.m

@synthesize tablePicture;

...

-(void)dealloc{
    [tablePicture release];
    [super dealloc];
}

【讨论】:

  • 这很好,但我似乎只为每个相应的表格单元格重复了第一个“pic1.png”
【解决方案2】:

在视图控制器的 .h 文件中为数组声明一个属性:

 @property (nonatomic, retain) NSArray *tablePicture;

在视图控制器的 .m 文件的 ViewDidLoad 方法中设置数组属性的值:

@synthesize tablePicture;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];
}

【讨论】:

  • 感谢它有一半的工作,但不只是“pic1.png”重复自己...见上面的编辑
  • 您的 tableView 中有多个部分吗?如果还可以查看您对 numberOfSectionsInTableView:numberOfRowsInSection: 的实现也很有用
  • - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.tableData.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; }
【解决方案3】:

听起来问题可能不在您的数组初始化范围内。

你为什么不尝试添加:

NSLog(@"%@",self.tablePicture);

行后

"self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil];"

【讨论】:

  • 我试过了,但仍然只有 pic1.png 重复自己:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 2012-05-22
  • 2014-03-03
  • 1970-01-01
  • 2022-07-21
相关资源
最近更新 更多