【问题标题】:how to change image when clicked on tableview cell iphone单击tableview单元iphone时如何更改图像
【发布时间】:2010-10-30 15:31:00
【问题描述】:

我有一个表格视图。当使用 cell.conteview addsubview 在 didselectrow 方法中单击单元格时,我将图像添加到单元格中。但问题是,如果我单击第一个单元格,它会更改图像,当我单击另一个单元格图像时,会出现但旧图像不会从前一个单元格中删除。如果单击单元格,则表格视图中的所有单元格都会发生这种情况。 我使用的代码如下

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    Practices *bPractices = [topics objectAtIndex:indexPath.row];
    UIImageView *clickView;
    //[cell.contentView removeFromSuperview];
    clickView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 315, 82)];
    clickView.image = [UIImage imageNamed:@"list_bg_hover.png"]; 
    [cell.contentView addSubview:clickView];
    [clickView release];

    UILabel *labelText= [[UILabel alloc]initWithFrame:CGRectMake(90, 30, 320, 20)];
    labelText.text=bPractices.practices_title;
    labelText.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:labelText];

}   

请帮我解决这个问题

提前致谢

【问题讨论】:

  • 你在重复使用单元格吗?可能是你的问题...在索引路径方法中发布你的单元格

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

我做了类似的事情,但不是用图像而是按钮。如果一个单元格尚未被选中并被标记,则大小会更改并添加特定数量的按钮。如果选择了另一个单元格,则此单元格将关闭。

来自 UITableViewController 的代码

界面:

int openedCellIndex;//<-what cell is selected
int buttonCounter;  

实现:

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

    if ( indexPath.row != openedCellIndex )
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                           reuseIdentifier:CellIdentifier] 
                    autorelease];
            //cell.frame = CGRectMake (0,0, 320, 100);
        }

        id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];

        cell.textLabel.text = article.name;

        if ( self.cellBackgroundColor )
            cell.backgroundColor = self.cellBackgroundColor;

            return cell;
    }
    else {

        //get article
        id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];

        //number of buttons
        int buttons = [article.specification count];
        int height = 50 * ceil(buttons / 2) + 50;

        //construct special cell
        UITableViewCell *cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                                        reuseIdentifier:@"CellBig"] 
                                 autorelease];
        cell.frame = CGRectMake (0,0, 320, 150);
        cell.textLabel.text = @"";

        //add label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 30)];
        label.font = [UIFont boldSystemFontOfSize:17];
        label.text = article.name;
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        [label release];

        if ( buttonMapper == nil )
            self.buttonMapper = [NSMutableDictionary dictionaryWithCapacity:10];
            //NSLog (@" bla: %@", article.isFreePrice);

            //see if we have a free prized article

            //create the buttons
            NSEnumerator *enumerator = [article.specification objectEnumerator];
            id <P_E_P1ArticleSpecification> spec;
        int count = 0;

        NSArray *specs = 
        [self sortedArticleSpecifications:article];

        for ( spec in specs ) {

            //see which row and col the button is in
            int row = floor ( count / 2 );
            int col = count % 2;

            //define button position
            int left = 20 + col * 145;
            int top = 45 + row * 50;

            //create button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(left, top, 135, 40);
            [cell addSubview:button];
            [button setTitleColor:[UIColor blackColor] forState:0];

            [button addTarget:self 
                       action:@selector(buttonTapped:) 
             forControlEvents:UIControlEventTouchUpInside];


            //remember which article the button is attached to
            buttonCounter++;

            button.tag = buttonCounter;

            [buttonMapper setValue:spec forKey:[NSString stringWithFormat:@"bla%d",buttonCounter]];

            count++;
        }


        if ( self.cellBackgroundColor )
            cell.backgroundColor = self.cellBackgroundColor;

            return cell;
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( openedCellIndex == indexPath.row )
    {
        id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];
        int count = [article.specification count];

        int height = 50 * ceil(count / 2.) + 50;

        return height;
    }

    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [self openRow:indexPath.row];
    [self.searchBar resignFirstResponder];


}

- (void) openRow:(int) index
{
    if ( index == openedCellIndex ) return;

    int oldIndex = openedCellIndex;
    openedCellIndex = index;


    NSUInteger indexArr[] = {0, oldIndex};
    NSIndexPath *oldPath = [NSIndexPath indexPathWithIndexes:indexArr length:2];

    NSUInteger indexArr2[] = {0, openedCellIndex};
    NSIndexPath *newPath = [NSIndexPath indexPathWithIndexes:indexArr2 length:2];


    //update view
    [(UITableView *)self.view beginUpdates];    
    if ( oldIndex >= 0 )
        [(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:oldPath] 
                                        withRowAnimation:UITableViewRowAnimationFade];

    if (openedCellIndex >=0 )
        [(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:newPath] 
                                        withRowAnimation:UITableViewRowAnimationFade];
    [(UITableView *)self.view endUpdates];
}

【讨论】:

    【解决方案2】:

    您也可以继承UITableViewCell 并在setSelected 方法中处理选择/取消选择过程。然后使用您的自定义单元格作为原型单元格的类型,而不是默认类型。

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多