自定义UITableViewCellAccessoryCheckmark

来源:互联网 发布:mysql下载64位 编辑:程序博客网 时间:2024/04/19 18:14

头文件:

Ios代码 复制代码 收藏代码
  1. #import <UIKit/UIKit.h>   
  2.   
  3. @interface TableView : UITableViewController {   
  4.     NSMutableArray *dataArray;   
  5. }   
  6.   
  7. @property (nonatomic, retain) NSMutableArray *dataArray;   
  8. @end  

 

实现文件:

Ios代码 复制代码 收藏代码
  1. #import "TableView.h"  
  2.   
  3. @implementation TableView   
  4.   
  5. @synthesize dataArray;   
  6.   
  7. - (void)viewDidLoad   
  8. {   
  9.     NSString *path = [[NSBundle mainBundle] pathForResource:@"CheckMark" ofType:@"plist"];   
  10.     self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];   
  11. }   
  12.   
  13. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  14. {   
  15.     return [dataArray count];   
  16. }   
  17.   
  18. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
  19. {   
  20.     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];   
  21.     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];   
  22. }   
  23.   
  24. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  25. {   
  26.     static NSString *kCustomCellID = @"MyCellID";   
  27.        
  28.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];   
  29.     if (cell == nil)   
  30.     {   
  31.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];   
  32.         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
  33.         cell.selectionStyle = UITableViewCellSelectionStyleBlue;   
  34.     }   
  35.        
  36.     NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];   
  37.     cell.textLabel.text = [item objectForKey:@"text"];   
  38.        
  39.     [item setObject:cell forKey:@"cell"];   
  40.        
  41.     BOOL checked = [[item objectForKey:@"checked"] boolValue];   
  42.     UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];   
  43.        
  44.     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];   
  45.     CGRect frame = CGRectMake(0.00.0, image.size.width, image.size.height);   
  46.     button.frame = frame;   
  47.        
  48.     [button setBackgroundImage:image forState:UIControlStateNormal];   
  49.        
  50.     [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];   
  51.     button.backgroundColor = [UIColor clearColor];   
  52.     cell.accessoryView = button;   
  53.        
  54.     return cell;   
  55. }   
  56.   
  57. - (void)checkButtonTapped:(id)sender event:(id)event   
  58. {   
  59.     NSSet *touches = [event allTouches];   
  60.     UITouch *touch = [touches anyObject];   
  61.     CGPoint currentTouchPosition = [touch locationInView:self.tableView];   
  62.     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];   
  63.     if (indexPath != nil)   
  64.     {   
  65.         [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];   
  66.     }   
  67. }   
  68.   
  69. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath   
  70. {      
  71.     NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];   
  72.        
  73.     BOOL checked = [[item objectForKey:@"checked"] boolValue];   
  74.        
  75.     [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];   
  76.        
  77.     UITableViewCell *cell = [item objectForKey:@"cell"];   
  78.     UIButton *button = (UIButton *)cell.accessoryView;   
  79.        
  80.     UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];   
  81.     [button setBackgroundImage:newImage forState:UIControlStateNormal];   
  82. }   
  83.   
  84. - (void)viewDidUnload   
  85. {   
  86.     self.dataArray = nil;   
  87. }   
  88.   
  89. - (void)dealloc   
  90. {      
  91.     [dataArray release];   
  92.     [super dealloc];   
  93. }   
  94.   
  95. @end  

 

示例图: