2013年3月14日 星期四

在TableViewCell的前後加入按鈕

//加在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath裡

//加入按鈕在右邊的cell.accessoryView
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(tipAction:) forControlEvents:UIControlEventTouchDown];
button.tag=indexPath.row+1;
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(20.0, 20.0, 60.0, 40.0);
cell.accessoryView = button;

//加入按鈕在左邊的cell.imageView
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton setFrame:CGRectMake(10,10,infoButton.frame.size.width, infoButton.frame.size.height)];
[infoButton addTarget:self action:@selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:infoButton];
[cell.imageView setImage:[UIImage imageNamed:@"info-button.png"]]; [cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoImageTapped:)];
[tap setNumberOfTapsRequired:1];
[cell.imageView setGestureRecognizers:[NSArray arrayWithObject:tap]];
[tap release];


//根據不同行的按鈕,利用tag做出不同反應
-(void)tipAction:(id)sender {
NSInteger tag= ((UIControl*)sender).tag; //為了讓compiler認出tag,先對sender參數轉型為UIControl
NSLog(@"%d",tag);
}

沒有留言:

張貼留言