2012-11-06 110 views
5

我有一個自定義的UITableViewCell,有很多按鈕,文本框,標籤和視圖。我將它們隱藏並顯示在每個索引路徑所需的位置。但由於某些原因,行不可點擊!UITableViewCell不可點擊

我甚至通過代碼做了以下事情,以確保如果在IB中發生了某些事情,它可以通過這些線路修復但無用。

cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
cell.userInteractionEnabled = YES; 

我想知道隱藏的意見是罪魁禍首嗎?

+1

iOS HIG:*「應用程序響應手勢,而不是點擊」* http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/Characteristics/Characteristics.html#//apple_ref/doc/uid/TP40006556-CH7-SW4 – Felix

+0

@ phix23 - 是啊,但它是如何應用於我的情況? – itsaboutcode

+1

組自定義對象userInteractionEnabled沒有 – NANNAV

回答

1

如果你的表視圖是可編輯的,這可能是爲什麼細胞是不能點擊

的理由,如果是這樣的話,你確實需要的細胞可以點擊編輯時 - 只是這樣做:

tableView.allowsSelectionDuringEditing = YES; 
+0

我可能聽起來很愚蠢,但我可以看到tableview是否可編輯? – itsaboutcode

+0

默認情況下,表格視圖不可編輯。簡單的方法是,當表格處於編輯模式時,您會看到紅色圓圈的減號或表格單元旁邊的綠色圓圈的加號。您可以編輯表設置「編輯」屬性設置爲TRUE,但你也應該實施以定義編輯風格和編輯處理本身的一些額外的方法,你可以在這裏讀到它:http://developer.apple.com/library /ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006943-CH3-SW58 – marika

0

實現此方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
+0

我已經實現這個方法了。 – itsaboutcode

6

試試這個tableView.allowsSelection=YES;

,你可以實現-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:

1

在你的自定義UITableViewCell中,你是否添加btn,textfields等到contentView?

也許如果你把所有內容添加到contentView,那應該可以解決你的問題。 希望它有幫助。

+0

請分享一些代碼。你如何創建自定義的UITableViewCell? –

2

這是一個自定義UITableView

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

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

} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

UILabel *lbl = [[UILabel alloc]initWithFrame:cell.frame]; 
lbl.backgroundColor = [UIColor clearColor]; 

UIImageView *cellImage = [[UIImageView alloc]initWithImage: [arrayImages objectAtIndex:indexPath.row]]; 
if (pointRange.location!=NSNotFound) { 


    cellImage.frame = CGRectMake(25,12.5, 75, 75); 
    lbl.frame = CGRectMake(0, 0, tableView.frame.size.width, 100); 
    lbl.font = [UIFont fontWithName:@"PineappleDemo" size:30.0]; 
} 

lbl.textColor =[UIColor colorWithRed:38.0/255.0 green:34.0/255.0 blue:98.0/255.0 alpha:1.0]; 
lbl.text =[arrayData objectAtIndex:indexPath.row]; 




for(UIView *v in [cell subviews]) 
{ 
    if([v isKindOfClass:[UILabel class]]||[v isKindOfClass:[UIImageView class]]||[v isKindOfClass:[UIButton class]]) 
     [v removeFromSuperview]; 
} 
if (index == indexPath.row) { 

    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 90, tableView.frame.size.width-200, 100)]; 
    lbl2.backgroundColor = [UIColor clearColor]; 
    lbl2.numberOfLines = 0; 
    lbl2.text = [arrayOffers objectAtIndex:indexPath.row]; 
    [cell addSubview:lbl2]; 
    [lbl2 release]; 

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn.frame = CGRectMake(tableView.frame.size.width-150, 100, 120, 84); 
    btn.backgroundColor = [UIColor clearColor]; 

    if (indexPath.row<=2) { 
     switch (indexPath.row) { 
      case 0: 
       [btn setImage:[UIImage imageNamed:@"lickFB.png"] forState:UIControlStateNormal]; 

       [btn addTarget:self action:@selector(facebook) forControlEvents:UIControlEventTouchUpInside]; 
       break; 
      case 1: 
       [btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal]; 

       [btn addTarget:self action:@selector(twitter) forControlEvents:UIControlEventTouchUpInside]; 
       break; 
      case 2: 

       [btn setImage:[UIImage imageNamed:@"rateUs.png"] forState:UIControlStateNormal]; 

       [btn addTarget:self action:@selector(rateAction) forControlEvents:UIControlEventTouchUpInside]; 

       break; 

      default: 
       break; 
     } 

    } 
    else{ 
     [btn setImage:[UIImage imageNamed:@"Buy_Button.png"] forState:UIControlStateNormal]; 
     switch (indexPath.row) { 
      case 3: 
       [btn addTarget:self action:@selector(deal1) forControlEvents:UIControlEventTouchUpInside]; 
       break; 
      case 4: 
       [btn addTarget:self action:@selector(deal2) forControlEvents:UIControlEventTouchUpInside]; 
       break; 
      case 5: 
       [btn addTarget:self action:@selector(deal3) forControlEvents:UIControlEventTouchUpInside]; 
       break; 

      case 6: 
       [btn addTarget:self action:@selector(deal4) forControlEvents:UIControlEventTouchUpInside]; 
       break; 



      default: 
       break; 
     } 

    } 
    [cell addSubview:btn]; 

} 

[cell addSubview:lbl]; 
[cell addSubview:cellImage]; 
[cellImage release]; 
[lbl release]; 
return cell; 

} 
3

我用了很多在我UITableViewCells不同的UI元素太大,爲什麼不嘗試和子類的UITableViewCell並推出自己的類,以項目爲屬性的示例細胞?似乎爲我工作。

在VincentsAwesomeCell.h

@interface VincentsAwesomeCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *FooLabel; 
@property (weak, nonatomic) IBOutlet UILabel *BarLabel; 
@property (weak, nonatomic) IBOutlet UIImageView *ShimmyIcon; 
@property (weak, nonatomic) IBOutlet UILabel *DooWopLabel; 
@property (weak, nonatomic) IBOutlet UIImageView *UseIcon; 
@property (weak, nonatomic) IBOutlet UILabel *TheForceLabel; 
@property (weak, nonatomic) IBOutlet UIImageView *bobSledIcon; 
@property (weak, nonatomic) IBOutlet UILabel *WristWatchLabel; 
@property (weak, nonatomic) IBOutlet UILabel *MeatLoafLabel; 
@property (weak, nonatomic) IBOutlet UILabel *SciFiLabel; 
@property (weak, nonatomic) IBOutlet UILabel *CollinderLabel; 
@property (weak, nonatomic) IBOutlet UILabel *RhubarbLabel; 
@property (weak, nonatomic) IBOutlet UITextField *SuperTextField; 
@property (weak, nonatomic) IBOutlet UIStepper *stepper; 
@property (strong, nonatomic) IBOutlet UILabel *amountOfAwesomeness; 

不要忘記使用:

在VincentsAwesomeCell.m:

- (void)prepareForReuse { 
    [super prepareForReuse]; 
    // Always call this! 
    // Reset custom stuff 
} 

重置細胞的屬性重用。

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Register the custom VincentsAwesomeCell NIB with the tableview 
    UINib *nib = [UINib nibWithNibName:@"VincentsAwesomeCell" bundle:nil]; 
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"VincentsAwesomeCell"]; 
} 

對於

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

我用這一個UIStepper跟蹤哪個被壓例如步進:

[[cell stepper]setTag:indexPath.row]; 
    [[cell stepper]addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged]; 

在TableViewController.m

然後

希望這會有所幫助!

P.S.

我使用的.xib文件創建的佈局設計我的自定義單元格的。只需將UITableViewCell拖入InterFaceBuilder並自定義即可!不要忘記鏈接屬性!

0

,如果你不能真正使「didSelectRowAtIndexPath方法」觸發得當,把一個大的UIButton佔據全細胞和做按鈕點擊必要的行動。只是一個想法! :-)

+0

謝謝。但這不可能,因爲單元格中有文本字段。 – itsaboutcode

+0

沒問題,你可以在所有控制元素後面有按鈕,這樣只有在沒有其他元素響應時纔會觸發。 –

0

我有類似的問題。解決它,但添加

self.clipsToBounds = YES; 

到我的tableView所在的視圖。所以我決定,我的tableView超出了其父視圖的框架。