0

Here's my setup. Table view with custom designed cell, reused many times. The layout is four UILabels and one UIImageView.

I load the cells in tableView:cellForRowAtIndexPath:, and inside it, after finding out which cell (data item) it is, I populate the labels with appropriate text from the datasource.

Also I have 7 pictograms. Each cell gets one of those pictograms, based on the cell's content (item type). The problem is, each pictogram exists in its normal version and its "selected" version. That means when the cell is selected, the design team has provided me with a modified version of pictogram with slightly different colours.

So for each cell I need to load pictogram_normal and pictogram_selected into the same cell so that the selected one is shown when the cell is selected.

How should I set this up?

4

1 回答 1

0

当一个单元格被选中时,改变这个单元格的背景图像视图,然后使其他单元格的背景图像视图正常。



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell=[tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pictogram_selected.png"]] autorelease];
    cell.selected=YES;
    selectedIndex=indexPath.row;

    for(UITableViewCell* theCell in [tableView visibleCells]){
        if(![theCell isEqual:cell])
        {
            theCell.accessoryType=UITableViewCellAccessoryNone;
            theCell.selected=NO;
            [theCell.backgroundView removeFromSuperview];
        }
    }
}

selectedIndex是一个保存 tableview 的 selectedindex 的数字。如果您想在 tableview 中保存所选状态,请使用 selectedIndex 使单元格被选中。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ITableViewCell*   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    //your code here

    if(indexPath.row == selectedIndex)
    {
        cell.backgroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pictogram_selected.png"]] autorelease];
        cell.selected=YES;
    }

    //your code here

    return cell;
}
于 2012-10-07T16:47:48.683 回答