2013-07-09 145 views
0

我想創建自動處理文本字段填充的自定義表格單元格。我的想法是,我只是將一個對象傳遞給單元類,然後單元格將自動填充字段。一切工作正常,除了單元格內沒有按鈕工作,它們都是不可點擊的。我究竟做錯了什麼?自定義可重用UITableViewCell

主屏:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSInteger row = [indexPath row]; 
     Claim *claim = [statementsArray objectAtIndex:row]; 
     NSString * strIndentifier; 

     strIndentifier = @"StatementDetailsCellIdentifier"; 

     StatementDetailsCell *cell = (StatementDetailsCell *) [tableView dequeueReusableCellWithIdentifier:strIndentifier]; 


     cell.hasWarranty = claim.hasWarranty; 
     if(cell == nil) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"StatementDetailsCell" owner:self options:nil]; 
      cell = [statementCell initWithClaim:claim reuseIdentifier:strIndentifier]; 

      cell.selectionStyle = UITableViewCellSelectionStyleNone;   
     } 

      return cell; 
    } 

Cell.m:

-(id)initWithClaim:(Claim *)_claim reuseIdentifier:(NSString *)reuseIdentifier 
    { 
     self = [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 
     claim = _claim; 

     [self populate]; 
     return self; 
    } 

    -(void)populate 
    { 
     barcodeLabel.text = claim.barcode; 

     NSLog(@"claim is %@", [claim description]); 

     if(claim.points == 0 || claim.points == 0.00) 
      valueLabel.text = @"Pending"; 
     else 
      valueLabel.text = [NSString stringWithFormat:@"£%.2f", claim.points]; 

     modelLabel.text = claim.product; 
     warrantyLabel.text = claim.warranty.name; 
     APIRequest *apiRequest = [[APIRequest alloc] init]; 
     dateLabel.text = [apiRequest parseDate:claim.date]; 
     //hasWarranty = claim.hasWarranty; 

     double timeS = [apiRequest getUnixTimestamp:claim.date]; 
     NSDate *now = [NSDate date]; 
     NSDate *trueDate = [NSDate dateWithTimeIntervalSince1970:timeS]; 
     double timeDiffrece = [now timeIntervalSinceDate:trueDate]; 
     double threemonths = 90*24*3600; 

     //Warranty Button 

     if(claim.hasWarranty) 
     { 
      UIImage *buttonImage; 
      //13-6-2013 
      if([claim.warranty.name isEqualToString:@"Pending"]) { 
       buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"]; 
       pendingHelp.hidden = NO; 
      } else { 
       pendingHelp.hidden = YES; 
       buttonImage = [UIImage imageNamed:@"warranty_claimed.png"]; 
      } 
      [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
      [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted]; 
      [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
     } 
     else 
     { 
      pendingHelp.hidden = YES; 
      if(false) { 
       [warrantyButton setBackgroundImage:nil forState:UIControlStateNormal]; 
       [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
      } else { 
       [warrantyButton setTag:claim.ID]; 
       UIImage *buttonImage = [UIImage imageNamed:@"add_warranty.png"]; 
       [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
       [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted]; 
       [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown]; 
       warrantyButton.userInteractionEnabled = YES; 
       warrantyButton.enabled = YES; 

      } 
     } 
    } 
+0

這是幹什麼用的? 'if(false){' – Tala

+0

這只是一種繞過某些預期邏輯的臨時手段。 –

+0

你有沒有初始化warrantyButton,否則所有的代碼將默默無法正常工作 – Tala

回答

0

可能是你的目標會從方法去除。請在填充方法的分類中指定一箇中斷點,並在目標分配用於檢查時嘗試顯示或隱藏warrantyButton。並仔細檢查圖像的名稱。

if(claim.hasWarranty) 
    { 
     UIImage *buttonImage; 
     //13-6-2013 
     if([claim.warranty.name isEqualToString:@"Pending"]) { 
      buttonImage = [UIImage imageNamed:@"imgBtnWarrantyPending.png"]; 
      pendingHelp.hidden = NO; 
     } else { 
      pendingHelp.hidden = YES; 
      buttonImage = [UIImage imageNamed:@"warranty_claimed"]; 
     } 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted]; 
     [warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 
     [warrantyButton setHidden:YES]; 
    } 
    else 
    { 
     pendingHelp.hidden = YES; 

     [warrantyButton setTag:claim.ID]; 
     UIImage *buttonImage = [UIImage imageNamed:@"add_warranty"]; 
     [warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     [warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted]; 
     [warrantyButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchDown]; 
     warrantyButton.userInteractionEnabled = YES; 
     warrantyButton.enabled = YES; 
     [warrantyButton setHidden:NO]; 

    } 

可能是它永遠不會進入分配目標的其他部分。我希望你能更好地理解它。

+0

試過。當使用斷點時,它會轉到addTarget行。奇怪的是它正確地設置了setBackgroundImage,但單元格上的no按鈕是可點擊的(我也使用了userInteractionEnabled) –

+0

它是否會進入其他部分但不會進入if(false){} else {}用於添加目標。所以直到除非你沒有指定任何目標,那麼它怎麼可以點擊。請嘗試更新我的代碼 – chandan

0

我相信你正試圖複製Sensible TableView框架已經做的事情。我建議你先檢查一下。