網頁

2012年12月10日 星期一

Xcode add a button with text in a UITableViewCell

button於cell上 但取得點選cell的ID (利用button.tag) 前面的圖會隨內容改變 加上的button可以加文字 並與button連動
- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    testArray = [[NSArray alloc] initWithObjects:@"test1",@"test2",@"vocab", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [testArray count];
}

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

    //cell.imageView
  
    NSString *iapClassImg =  [[testArray objectAtIndex:indexPath.row] isEqualToString:@"vocab"] ?  @"test.png" : @"vocab.png";
    UIImage *imageCellImageView = [UIImage imageNamed: iapClassImg];
    cell.imageView.image = imageCellImageView;
    
    //cell.accessoryView
    UIButton *restoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 
    UIImage *downloadButtonImage = [UIImage imageNamed: @"restore.png"]; 
    [restoreButton setBackgroundImage: downloadButtonImage forState: UIControlStateNormal];
    [restoreButton setFrame:CGRectMake(0, 0, downloadButtonImage.size.width/7, downloadButtonImage.size.height/7)];
    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 = [testArray objectAtIndex:indexPath.row]; ////indexID;
    
    buttonIAP= [UIButton buttonWithType:UIButtonTypeCustom];
    buttonIAP.tag = indexPath.row;
    
    
    buttonIAP.titleLabel.font = [UIFont systemFontOfSize:32];
    [buttonIAP setTitle:@"$0.99" forState:UIControlStateNormal];
    [buttonIAP addSubview:labelPrice];
    
    UIImage *lockButtonImage = [UIImage imageNamed: @"btn_price.png"];
    buttonIAP.frame = CGRectMake(430, 5, lockButtonImage.size.width, lockButtonImage.size.height);
    [buttonIAP setBackgroundImage: lockButtonImage forState: UIControlStateNormal];
    [buttonIAP addTarget:self action:@selector(btnIAPunlock:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell addSubview:buttonIAP];
    
    return cell;
}

- (IBAction)btnIAPunlock:(id)sender
{
    UIButton *btnUnlock = sender;
    NSLog(@"btnUnlock.tag = %d", btnUnlock.tag);
    
  
    UIImage *lockButtonImage = [UIImage imageNamed: @"btn_installed.png"];

    btnUnlock.frame = CGRectMake(430, 5, lockButtonImage.size.width, lockButtonImage.size.height);
    [btnUnlock setBackgroundImage: lockButtonImage forState: UIControlStateNormal];
    [btnUnlock setTitle:@"" forState:UIControlStateNormal]; 
}


沒有留言:

張貼留言

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