【发布时间】:2011-09-07 21:02:24
【问题描述】:
对于我正在开发的 iPhone 应用程序,我需要根据数组中任意项的整数来分配自定义的 uitableviewcell 图像。我注意到在做一些测试时 indexPath of row 只返回显示视图的索引,所以基本上我需要知道如何获取一个任意大小的数组,假设现在它有 10 个项目,这 10 个项目在一个表,每个项目一个单元格,我需要每个项目都有一个索引,比如项目 1 是索引 0,项目 2 是索引 1,依此类推。所以我的代码会读取,如果索引 == 0,则在单元格中显示此图像,如果索引!= 0 则显示另一个。我试过了,但就像我说的,如果我滚动到我的表格视图的底部,它会将视图顶部的任何表格项重新分配为 0,所以当我滚动图像时,图像会不断变化。所以基本上我需要帮助根据我的数组中的索引而不是表的索引来分配图像。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
}
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 1;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *title =[[stories objectAtIndex: indexPath.row] objectForKey: @"summary"];
NSString *title2 =[[stories objectAtIndex: indexPath.row] objectForKey: @"title"];
NSString *dayOfMonthString = [title substringWithRange: NSMakeRange(2, 2)];
int dateChooser = [dayOfMonthString intValue];
NSString *monthString = [title substringWithRange: NSMakeRange(0, 2)];
int monthChooser = [monthString intValue];
cell.textLabel.text =title2;
cell.imageView.image =
[UIImage imageNamed: [NSString stringWithFormat: @"cal%d.png", dateChooser]];
if (monthChooser == 1 && (INDEX COMPARISON CODE???){
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Jan1.png"]];
[cell setBackgroundView:myImageView];
}
【问题讨论】:
-
请帮我们一个忙并发布您的代码。不是全部,只是相关的部分。
-
我可以推荐这篇文章吗:stackoverflow.com/questions/1135075/…
-
那篇文章并不适用,因为无论大小,我都需要数组中每个项目的索引,而不是数组中单个对象的索引
标签: objective-c uitableview nsarray