2013-05-31 114 views
1

我M個新的,我添加此代碼到定製表格視圖與圖像和按鈕行顯示按鈕

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

    static NSString *CellIdentifier = @"ImageOnRightCell"; 
    UILabel *mainLabel, *secondLabel; 
    UIImageView *photo; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) 
    { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 0.0, 100, 20)]; 
     [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [button setTag:1]; 
     [cell.contentView addSubview:button]; 

     photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)]; 
     photo.tag = PHOTO_TAG; 


     photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 

     [cell.contentView addSubview:photo]; 
    } 

    UIImage *theImage = [UIImage imageNamed:@"man.jpg"]; 
    photo.image = theImage; 

    return cell; 

} 

圖像部分在表中的行的到來,而不是按鈕。 是什麼,請在它的問題

+1

請修復代碼格式。 –

+0

嘗試給按鈕標題或背景顏色。其實一個自定義單元格是不錯的選擇 – Durgaprasad

+0

問題是與按鈕的框架。部分按鈕位於imageView後面。按鈕150的改變的x座標和看到 –

回答

2

試試吧....

UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeCustom]; 
finalPriceBtn.tag=i+200; 
finalPriceBtn.frame = CGRectMake(100, 0.0, 100, 20); 
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside]; 
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12]; 
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal]; 
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateSelected]; 
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateNormal]; 
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft; 
[cell.contentView addSubview:finalPriceBtn]; 

希望我幫助。

+0

其wrkin感謝名單 – user2256034

+0

如何我只選擇按鈕點擊表格視圖 – user2256034

+0

的不屬於細胞@ user2256034: - (IBAction爲)goBtnClk:(ID)發送方 { INT I = [發件人標籤]; i = i - 200; //選擇按鈕ID } –

1

要添加的按鈕,然後將圖像,所以圖像是在按鈕的上方,這樣做:

[cell.contentView addSubview:photo]; 

在此之前:

[cell.contentView addSubview:button]; 
1

您正在您的按鈕上方添加圖片,請將其添加到按鈕下方。

用你這樣的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ImageOnRightCell"; 
    UILabel *mainLabel, *secondLabel; 
    UIImageView *photo; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)]; 
     photo.tag = PHOTO_TAG; 
     photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:photo]; 

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(100, 0.0, 100, 20); 
     [button setBackgroundColor:[UIColor redColor]];// Here add some background color to check its visibility 
     [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button]; 
    } 

    UIImage *theImage = [UIImage imageNamed:@"man.jpg"]; 
    photo.image = theImage; 

    return cell; 
} 
+0

我應用了ur代碼,但按鈕沒有來 – user2256034

+0

檢查我的更新代碼。 –

+0

thanx向你求助 – user2256034