網頁

2012年11月27日 星期二

Xcode add a button in a UITableViewCell

button於cell上 但取得點選cell的ID (利用button.tag)
 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    

    //cell.imageView 
    NSString *fileNameCellImageView = [[NSString alloc] initWithFormat: @"test.png"];
    UIImage *imageCellImageView = [UIImage imageNamed: fileNameCellImageView]; 
    cell.imageView.image = imageCellImageView;
    
    //cell.accessoryView     
    UIButton *restoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //[restoreButton setTitle:@"restore" forState:UIControlStateNormal];
    UIImage *downloadButtonImage = [UIImage imageNamed: @"restore.png"];
    [restoreButton setBackgroundImage: downloadButtonImage forState: UIControlStateNormal];
    [restoreButton setFrame:CGRectMake(0, 0, downloadButtonImage.size.width/5, downloadButtonImage.size.height/5)];
    cell.accessoryView = restoreButton;
    
    [restoreButton addTarget: self action: @selector(btnRestore:) forControlEvents: UIControlEventTouchUpInside];

 
    //cell add your button
    NSString *indexID = [[NSString alloc]initWithFormat:@"%d",indexPath.row];
    cell.textLabel.text =indexID;
    
    buttonIAP= [UIButton buttonWithType:UIButtonTypeCustom];
    buttonIAP.tag = indexPath.row ;

    
    UIImage *lockButtonImage = [UIImage imageNamed: @"lock.png"];
    buttonIAP.frame = CGRectMake(400, 10, lockButtonImage.size.width/6, lockButtonImage.size.height/6);
    [buttonIAP setBackgroundImage: lockButtonImage forState: UIControlStateNormal];
    [buttonIAP addTarget:self action:@selector(btnIAPunlock:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:buttonIAP];
    
    //cell.textLabel.text = [testArray objectAtIndex:indexPath.row];
    return cell;
}

- (IBAction)btnIAPunlock:(id)sender
{
    UIButton *btnUnlock = sender;
    NSLog(@"btnUnlock.tag = %d", btnUnlock.tag);
   
    UIImage *lockButtonImage = [UIImage imageNamed: @"unlock.png"];
    btnUnlock.frame = CGRectMake(400, 10, lockButtonImage.size.width/6, lockButtonImage.size.height/6);
    [btnUnlock setBackgroundImage: lockButtonImage forState: UIControlStateNormal];

}

How to delete section from UITableView (iPhone/iPad)

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。