如何获取数组Objective-C中每个项的索引

时间:2011-09-07 21:02:24

标签: objective-c uitableview nsarray

对于我正在开发的iPhone应用程序,我需要根据数组中任意项的整数分配自定义的uitableviewcell图像。我注意到在做一些测试时,行的indexPath只返回所显示视图的索引,所以基本上我需要知道如何获取一个任意大小的数组,让我们说现在它有10个项目,这10个项目在表,每个项目一个单元格,我需要每个项目有一个索引,如项目1是索引0,项目2是索引1,依此类推。所以我的代码会读取,如果索引== 0,则在单元格中显示此图像,如果索引!= 0则显示另一个。我试过了,但就像我说的,如果我滚动到我的tableview的底部,它将重新分配视图顶部的任何一个表项为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];
    }

1 个答案:

答案 0 :(得分:4)

您的问题无法为正确的单元格获取正确的索引。 row 执行indexPath属性对应于整个单元格列表中单元格的索引,而不仅仅是可见单元格的索引,因此完全符合您的预期最初

我打赌你的问题是你没有正确使用UITableViewCells的重用机制 。 当您在TableView中滚动时,不再在屏幕上的UITableViewCells被“回收”并重新用于在屏幕上显示新的单元格,以避免过多的分配和无用的初始化,否则会减慢tableView的滚动速度

返回单元格的正确代码模式如下:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    // Try to retrieve a previously created cell that is not used anymore
    // That's the "recycling" part, we try to reuse a cell instead of creating a new one if possible
    UITableViewCell* cell = [tableView dequeueCellWithIdentifier:@"myIdentifier"];

    if (cell == nil)
    {
        // We failed to recycle a previously created cell, so we have no choice to allocate a new one
        cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:@"myIdentifier"] autorelase];
        // As we just created the cell, we configure everything that will be common to every cell
        // and that won't change even if the cell is reused later when you scroll

        // e.g. text color, text font, accessoryType, ...
        cell.textLabel.textColor = [UIColor blueColor];
        ...
    }

    // Now we got here either with a brand new cell that have just been created…
    // … or after reusing an old cell that were not onscreen and has been recycled
    // So THIS IS THE PLACE to configure everything that is SPECIFIC to each cell

    // namely cell text, especially
    cell.textLabel.text = [yourDataArray objectAtIndex: indexPath.row];


    return cell;
}