代码之家  ›  专栏  ›  技术社区  ›  rein

UITableViewCell中的复选框图像切换

  •  29
  • rein  · 技术社区  · 17 年前

    我需要一些关于创建一个 UITableViewCell

    我正在努力解决的部分是:

    1. 如何检测图像上的点击并以不同方式处理 didSelectRowAtIndexPath ?
    2. 如何更改图像而不执行 [tableView reloadData]
    6 回复  |  直到 7 年前
        1
  •  28
  •   Amagrammer    17 年前

    其实很简单。

    只需创建一个新的UIControl子类并将其全部放入其中(不需要单独的控制器)。我们将其称为ToggleImageControl。

    @interface ToggleImageControl : UIControl
    {
       BOOL selected;
       UIImageView *imageView;
       UIImage *normalImage;
       UIImage *selectedImage;
    }
    

    为每个单元格创建一个ToggleImageControl,并将其添加到适当的位置。

    ToggleImageControl *toggleControl = [[ToggleImageControl alloc] initWithFrame: <frame>];
    toggleControl.tag = indexPath.row;  // for reference in notifications.
    [cell.contentView addSubview: toggleControl];
    

    - (void) viewDidLoad
    {
       normalImage = [UIImage imageNamed: @"normal.png"];
       selectedImage = [UIImage imageNamed: @"selected.png"];
       imageView = [[UIImageView alloc] initWithImage: normalImage];
       // set imageView frame
       [self.view addSubview: imageView];
    
    [self addTarget: self action: @selector(toggleImage) forControlEvents: UIControlEventTouchUpInside];
    
    }
    

    设置UIImageView的image属性以更新图像;这将在没有副作用的情况下触发重新绘制。

    - (void) toggleImage
    {
       selected = !selected;
       imageView.image = (selected ? selectedImage : normalImage); 
    
       // Use NSNotification or other method to notify data model about state change.
       // Notification example:
       NSDictionary *dict = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: self.tag forKey: @"CellCheckToggled"];
       [[NSNotificationCenter defaultCenter] postNotificationName: @"CellCheckToggled" object: self userInfo: dict];
    
    }
    

        2
  •  6
  •   Archie    15 年前

    下面是“override TouchesBegind”的一个实现:“我正在使用的方法很简单,而且似乎效果很好。

    只需在项目中包含此类,并创建和配置 TouchIconTableViewCell UITableView 你的手机 tableView:cellForRowAtIndexPath: 方法。

    TouchIconTableViewCell.h:

    #import <UIKit/UIKit.h>
    
    @class TouchIconTableViewCell;
    
    @protocol TouchIconTableViewCellDelegate<NSObject>
    
    @required
    - (void)tableViewCellIconTouched:(TouchIconTableViewCell *)cell indexPath:(NSIndexPath *)indexPath;
    
    @end
    
    @interface TouchIconTableViewCell : UITableViewCell {
        id<TouchIconTableViewCellDelegate> touchIconDelegate;       // note: not retained
        NSIndexPath *touchIconIndexPath;
    }
    
    @property (nonatomic, assign) id<TouchIconTableViewCellDelegate> touchIconDelegate;
    @property (nonatomic, retain) NSIndexPath *touchIconIndexPath;
    
    @end
    

    #import "TouchIconTableViewCell.h"
    
    @implementation TouchIconTableViewCell
    
    @synthesize touchIconDelegate;
    @synthesize touchIconIndexPath;
    
    - (void)dealloc {
        [touchIconIndexPath release];
        [super dealloc];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint location = [((UITouch *)[touches anyObject]) locationInView:self];
        if (CGRectContainsPoint(self.imageView.frame, location)) {
            [self.touchIconDelegate tableViewCellIconTouched:self indexPath:self.touchIconIndexPath];
            return;
        }
        [super touchesBegan:touches withEvent:event];
    }
    
    @end
    

    touchIconDelegate touchIconIndexPath 财产。触摸图标时,将调用代理。然后你可以更新图标或其他任何东西。

        3
  •  4
  •   sth    16 年前

    所以“.显然需要按摩一些东西…”注释的意思是“…这段代码不起作用…”。

    - (void) viewDidLoad
    

    应该是

    - (id)initWithFrame:(CGRect)frame 
    {
     if ( self = [super initWithFrame: frame] ){
      normalImage = [UIImage imageNamed: @"toggleImageNormal.png"];
      selectedImage = [UIImage imageNamed: @"toggleImageSelected.png"];
      imageView = [[UIImageView alloc] initWithImage: normalImage];
    
     // set imageView frame
      [self addSubview: imageView];
    
      [self addTarget: self action: @selector(toggleImage) forControlEvents: UIControlEventTouchDown];
     }
    
     return self;
    }
    

    作为 - (void) viewDidLoad

        4
  •  3
  •   tokentoken    13 年前

    此代码可以启用编辑模式下的多项选择。

    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    

    http://developer.apple.com/library/ios/#samplecode/TableMultiSelect/Introduction/Intro.html

    几个小时后,我在Stack Overflow中找不到这个示例代码,所以我在这篇文章中添加了这个信息。

        5
  •  1
  •   skylerl    16 年前

    [super touchesBegan:touches withEvent:event] 它将像被选中一样运行。

        6
  •  0
  •   TahoeWolverine    17 年前

    如果触摸在复选框上,您可能可以捕获didSelectRowAtIndexPath事件,然后执行一些其他逻辑,而不是重定向,尽管我不知道您如何获得该位置。这意味着您可能需要找到一种方法,在touch事件调用didselectrowatindexpath之前找到它,我不太确定该怎么做。你有没有接触过TouchesStart之类的东西?