아이폰어플개발정보2010. 10. 12. 04:54
테이블뷰에서 셀하나가 터치되었을때 체크표시하기 -Accessory in apple source

리스트뷰에서 체크되었을때



#import "MyTableViewController.h"


@interface MyTableViewController ()

@property (nonatomic, retain) NSMutableArray *dataArray;

@end


@implementation MyTableViewController


@synthesize dataArray;


- (void)viewDidLoad

{

// load our data from a plist file inside our app bundle

NSString *path = [[NSBundlemainBundle] pathForResource:@"tableData"ofType:@"plist"];

self.dataArray = [NSMutableArrayarrayWithContentsOfFile:path];

}



// called after the view controller's view is released and set to nil.

// For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.

// So release any properties that are loaded in viewDidLoad or can be recreated lazily.

//

- (void)viewDidUnload

{

self.dataArray = nil;

}



- (void)dealloc

{

    [dataArrayrelease];

[superdealloc];

}



#pragma mark - UITableView delegate methods


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [dataArraycount];

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[selftableView: self.tableViewaccessoryButtonTappedForRowWithIndexPath: indexPath];

[self.tableViewdeselectRowAtIndexPath:indexPath animated:YES];

}



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

{

static NSString *kCustomCellID = @"MyCellID";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:kCustomCellID];

if (cell == nil)

{

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);

button.frame = frame;// match the button's size with the image size

[button setBackgroundImage:image forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet

[button addTarget:selfaction:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColorclearColor];

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)

{

[selftableView: self.tableViewaccessoryButtonTappedForRowWithIndexPath: indexPath];

}

}



- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];

BOOL checked = [[item objectForKey:@"checked"] boolValue];

[item setObject:[NSNumbernumberWithBool:!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];

}


@end


#import <UIKit/UIKit.h>


@interface MyTableViewController : UITableViewController

{

@private

NSMutableArray *dataArray;

}


@end


Posted by 오늘마감

댓글을 달아 주세요