自定义UITableViewCellAccessoryCheckmark
来源:互联网 发布:mysql下载64位 编辑:程序博客网 时间:2024/04/19 18:14
头文件:
- #import <UIKit/UIKit.h>
- @interface TableView : UITableViewController {
- NSMutableArray *dataArray;
- }
- @property (nonatomic, retain) NSMutableArray *dataArray;
- @end
#import <UIKit/UIKit.h>@interface TableView : UITableViewController {NSMutableArray *dataArray;}@property (nonatomic, retain) NSMutableArray *dataArray;@end
实现文件:
- #import "TableView.h"
- @implementation TableView
- @synthesize dataArray;
- - (void)viewDidLoad
- {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"CheckMark" ofType:@"plist"];
- self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [dataArray count];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *kCustomCellID = @"MyCellID";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
- if (cell == nil)
- {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- cell.selectionStyle = UITableViewCellSelectionStyleBlue;
- }
- NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
- cell.textLabel.text = [item objectForKey:@"text"];
- [item setObject:cell forKey:@"cell"];
- BOOL checked = [[item objectForKey:@"checked"] boolValue];
- UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
- button.frame = frame;
- [button setBackgroundImage:image forState:UIControlStateNormal];
- [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
- button.backgroundColor = [UIColor clearColor];
- cell.accessoryView = button;
- return cell;
- }
- - (void)checkButtonTapped:(id)sender event:(id)event
- {
- NSSet *touches = [event allTouches];
- UITouch *touch = [touches anyObject];
- CGPoint currentTouchPosition = [touch locationInView:self.tableView];
- NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
- if (indexPath != nil)
- {
- [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
- }
- }
- - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
- {
- NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
- BOOL checked = [[item objectForKey:@"checked"] boolValue];
- [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
- UITableViewCell *cell = [item objectForKey:@"cell"];
- UIButton *button = (UIButton *)cell.accessoryView;
- UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
- [button setBackgroundImage:newImage forState:UIControlStateNormal];
- }
- - (void)viewDidUnload
- {
- self.dataArray = nil;
- }
- - (void)dealloc
- {
- [dataArray release];
- [super dealloc];
- }
- @end
#import "TableView.h"@implementation TableView@synthesize dataArray;- (void)viewDidLoad{NSString *path = [[NSBundle mainBundle] pathForResource:@"CheckMark" ofType:@"plist"];self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return [dataArray count];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];[self.tableView deselectRowAtIndexPath:indexPath animated:YES];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *kCustomCellID = @"MyCellID";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];if (cell == nil){cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;cell.selectionStyle = UITableViewCellSelectionStyleBlue;}NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];cell.textLabel.text = [item objectForKey:@"text"];[item setObject:cell forKey:@"cell"];BOOL checked = [[item objectForKey:@"checked"] boolValue];UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);button.frame = frame;[button setBackgroundImage:image forState:UIControlStateNormal];[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];button.backgroundColor = [UIColor clearColor];cell.accessoryView = button;return cell;}- (void)checkButtonTapped:(id)sender event:(id)event{NSSet *touches = [event allTouches];UITouch *touch = [touches anyObject];CGPoint currentTouchPosition = [touch locationInView:self.tableView];NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];if (indexPath != nil){[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];}}- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];BOOL checked = [[item objectForKey:@"checked"] boolValue];[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];UITableViewCell *cell = [item objectForKey:@"cell"];UIButton *button = (UIButton *)cell.accessoryView;UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];[button setBackgroundImage:newImage forState:UIControlStateNormal];}- (void)viewDidUnload{self.dataArray = nil;}- (void)dealloc{ [dataArray release];[super dealloc];}@end
示例图:
- 自定义UITableViewCellAccessoryCheckmark
- UITableViewCellAccessoryCheckmark标记的使用
- 表格单选实现UITableViewCellAccessoryCheckmark
- ios 关于UITableView UITableViewCellAccessoryCheckmark 混乱的问题
- tableView 中的单选控制 UITableViewCellAccessoryCheckmark
- tableView 中的单选控制 UITableViewCellAccessoryCheckmark
- UITableView中确保UITableViewCellAccessoryCheckmark的唯一性
- UITableViewCell 中的单选控制 UITableViewCellAccessoryCheckmark
- UITableViewCell 中的单选控制 UITableViewCellAccessoryCheckmark
- UITableViewCell 中的单选控制 UITableViewCellAccessoryCheckmark
- 改变UITableViewCellAccessoryCheckmark(UITableView Cell后面的对勾)的颜色
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义TexBox,自定义ComboBox
- 自定义View自定义属性
- reuse_alv_grid_display 工具条增强
- 对基于Lua和Nginx的iOS应用数据的加密框架
- function方式的ALV中 增加按钮 用 REUSE_ALV_GRID_DISPLAY
- 黑马程序员_函数
- GridView鼠标滑过变色
- 自定义UITableViewCellAccessoryCheckmark
- 简单学习向MFC对话框中导入.jpg图片
- table control sort
- 黑马程序员_面向对象
- 72小时内的2份兼职
- 黑马程序员_异常
- 【Visual C++】游戏开发笔记十九 DirectX与OpenGL的博弈
- Debian 安装相关
- 黑马程序员_多线程