UITableViewCell中的复选框图像切换

rei*_*ein 29 iphone cocoa-touch uitableview

我需要一些关于创建UITableViewCell的指导,左边有一个可以切换的图像.图像应该是可插拔的,并充当切换(复选框).

我正在努力的部分是:

  1. 如何检测图像上的点击并以不同的方式处理didSelectRowAtIndexPath?
  2. 如何在不执行[tableView reloadData]的情况下更改图像?

有关示例,请参见下图:

替代文字http://img208.yfrog.com/img208/6119/screenshotkmr.png

谢谢

Ama*_*mer 28

这实际上非常简单.

只需创建一个新的UIControl子类并将其全部放在那里(不需要单独的控制器.)让我们称之为ToggleImageControl.

@interface ToggleImageControl : UIControl
{
   BOOL selected;
   UIImageView *imageView;
   UIImage *normalImage;
   UIImage *selectedImage;
}
Run Code Online (Sandbox Code Playgroud)

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

ToggleImageControl *toggleControl = [[ToggleImageControl alloc] initWithFrame: <frame>];
toggleControl.tag = indexPath.row;  // for reference in notifications.
[cell.contentView addSubview: toggleControl];
Run Code Online (Sandbox Code Playgroud)

添加UIImageView以包含图像.添加触摸事件的目标.

- (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];

}
Run Code Online (Sandbox Code Playgroud)

设置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];

}
Run Code Online (Sandbox Code Playgroud)

你显然需要按摩一些东西.您可能希望传递两个图像名称以使其更具可重用性,并且我还建议您从对象外部指定通知名称字符串(假设您使用的是通知方法.)


Arc*_*hie 6

这是我正在使用的"覆盖touchesBegan:"方法的实现,这很简单,似乎运行良好.

只需在项目中包含此类,然后在方法中创建和配置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
Run Code Online (Sandbox Code Playgroud)

TouchIconTableViewCell.m:

#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
Run Code Online (Sandbox Code Playgroud)

每次创建或重新使用单元格时,请设置touchIconDelegatetouchIconIndexPath属性.触摸图标后,将调用该代理.然后你可以更新图标或其他什么.