2012-03-23 37 views
0

在我的UITableViewCell中有一個UIImageView和UIButton。UITableViewCell中UIView的UIButton沒有觸發事件

這裏我code

我的問題是,每個我嘗試點擊的UIButton的UITableViewCell的時間由TouchUpInside事件,而不是UIButton的觸發。

你能幫我嗎?

+0

UIView類必須的UITableViewCell導出,並使用方法表觀察室'addContentView'到元素添加到表視圖單元格 – iDroid 2012-03-24 05:46:45

+0

你的意思是[cell.contentView addSubview:]? – 2013-01-16 10:00:46

+0

您提供的鏈接沒有代碼! – NightFury 2013-09-01 12:45:27

回答

0

這是我的代碼..在這裏我用的是UITableViewCellUIButton和它的作品完美的我..希望這會幫助你..

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = @"ChildList"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    itsChildContactsTableView = tableView; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // we need a view to place our labels on. 
    UIView *aTableCellContentView = [[UIView alloc] initWithFrame:CGRectZero]; 



    CGRect contentRect = aTableCellContentView.bounds; 

    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 

    frame = CGRectMake(boundsX + 10, 8, 200, 20); 



    UIButton *aCallButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aCallButton setImage:[UIImage imageNamed:@"call.png"] forState:UIControlStateNormal]; 
    [aCallButton addTarget:self action:@selector(dialNumber:) forControlEvents:UIControlEventTouchUpInside]; 
    [aCallButton setTag:[indexPath row]]; 

    frame = CGRectMake(210, 14, 35, 35); 
    aCallButton.frame = frame; 

    UIButton *aEmailButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aEmailButton setImage:[UIImage imageNamed:@"mail.png"] forState:UIControlStateNormal]; 
    [aEmailButton addTarget:self action:@selector(eMailContact:) forControlEvents:UIControlEventTouchUpInside]; 


    [aEmailButton setTag:[indexPath row]]; 

    frame = CGRectMake(250, 14, 35, 35); 

    aEmailButton.frame = frame; 
    aTableCellContentView.backgroundColor = [UIColor whiteColor]; 
    [cell.contentView addSubview:aCallButton]; 
    [cell.contentView addSubview:aEmailButton]; 
    [cell.contentView addSubview:aTableCellContentView]; 
    [aTableCellContentView release]; 
    return cell; 
} 
0

如果你的UITableViewCell有一個客戶的高度(不。默認44.0f)如果是這樣,你應該設置這樣的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"cellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
     cell.contentView.frame = CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForRowAtIndexPath:indexPath]); 
     // add a UIButton that you create 
    } 
    return cell; 
} 

這個問題的原因是cell.contentView.size是默認(320.f,44.0f),如果你的按鈕通過矩形,那麼按鈕不能接受事件,所以你應該設置cell.con tentView.frame自己,就像我做的代碼。

好運。

+0

請不要在多個問題上發佈完全相同的答案:它不是很適合所有問題,或者問題是應該標記/關閉的重複問題。 – kleopatra 2013-09-01 12:37:26