您的位置:首页 > 移动开发 > IOS开发

IOS开发之TableView替换默认的checkmark为自定义图像

2013-08-02 22:28 453 查看
直接上代码:

On
cellForRowAtIndexPath:


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;

checkButtonTapped:event: Method:

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

accessoryButtonTappedForRowWithIndexPath: Method

-(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];}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐