2014-02-28 72 views
0

我有一個自定義單元格,它由3個不同的文本字段txtsq,txtPOB,txtRxunit和這個自定義單元格m在我的表格視圖控制器中,在表格視圖控制器類tableView cellForRowAtIndexPath內: (NSIndexPath *)indexPath,現在我想爲我設置3個不同的文本字段這是在自定義單元格的3種不同的範圍限制,但我不能設置範圍在我設置textFIeld的標記

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string{ 
    NSString *resultStr = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    if (textField.tag ==SQ) { 

    if([self isNumeric:resultStr]){ 
     if(resultStr.length <= 3){ 
     if(resultStr.length >= 1){ 
      NSString *firstLetter = [resultStr substringWithRange:NSMakeRange(0, 1)]; 
      if(![firstLetter isEqualToString:@"0"]){ 
      return YES; 
      } 
     } 
     else{ 
      return YES; 
     } 
     } 
    } 
    }else 

    if([self isNumeric:resultStr]){ 
    if(resultStr.length <= 5){ 
     if(resultStr.length >= 1){ 
     NSString *firstLetter = [resultStr substringWithRange:NSMakeRange(0, 1)]; 
     if(![firstLetter isEqualToString:@"0"]){ 
      return YES; 
     } 
     } 
     else{ 
     return YES; 
     } 
    } 
    } 

    return NO; 
} 

我已經定義cell.txtSQ.tag=SQ;

cell.txtPOB.tag =POB; 
cell.txtRxUnit.tag=RXunit; 

使用此代碼後,我的表視圖d阿塔並不持久。

  • (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {

如果(self.skuNameArray.count> 0){

OrderCell *cell = nil; 
    cell = (OrderCell *)[tableView dequeueReusableCellWithIdentifier:@"OrderCell"]; 

    if(cell == nil){ 

     cell = (OrderCell *)[OrderCell cellFromNibNamed:@"OrderCell"]; 
    } 

    // Set Tag 
    cell.tag   = indexPath.row; 
    cell.txtSQ.tag  = indexPath.row; 
    cell.txtPOB.tag  = indexPath.row; 
    cell.txtRxUnit.tag = indexPath.row; 

// cell.txtSQ.tag = SQ;

//set Delegate 
    cell.txtPOB.delegate = self; 
    cell.txtRxUnit.delegate = self; 
    cell.txtSQ.delegate = self; 

    //set Values 
    cell.lblSKU.text  = self.skuNameArray[indexPath.row]; 
    cell.txtSQ.text   = self.sqArray[indexPath.row]; 
    cell.txtPOB.text  = self.pobArray[indexPath.row]; 
    cell.txtRxUnit.text  = self.rxUnitArray[indexPath.row]; 


    if (self.index == PCP || self.index == CURRENT_DAY_REPORTING_DETAIL) { 

     cell.txtSQ.enabled = NO; 
     cell.txtRxUnit.enabled = NO; 
     cell.txtPOB.enabled = NO; 
    } 
     [cell.txtSQ  addTarget:self action:@selector(sqChanged:)  forControlEvents: UIControlEventEditingChanged]; 
     [cell.txtPOB addTarget:self action:@selector(pobChanged:) forControlEvents: UIControlEventEditingChanged]; 
     [cell.txtRxUnit addTarget:self action:@selector(rxUnitChanged:) forControlEvents: UIControlEventEditingChanged]; 





    cell.backgroundColor = [UIColor clearColor]; 

    return cell; 

} 
+0

的問題是不是標籤,但你的tableview重用細胞的事實。您必須確保cellforrowatindexpath方法使用正確的textview和標記填充當前單元格。也許你應該發佈該cellforrowatindexpath和你建立你的單元格的方式 –

+0

什麼是SQ?... –

+0

'shouldChange'方法是我昨天告訴的答案,我想如果你寫一個resultString的方法作爲參數會更好。也許你列出'cellForRowAtIndexPath'代碼將有助於儘快解決它。希望可以再次幫助你。 – simalone

回答

0

您的SQ可能是動態的。所以,把它變成靜態的。

您可能沒有將代表設置爲self。這樣做:

cell.txtSQ.delegate = self; 
cell.txtPOB.delegate = self; 
cell.txtRxUnit.delegate = self; 

精心設置您的標籤:

// Set Tag 
cell.txtSQ.tag  = SQ; 
cell.txtPOB.tag  = POB; 
cell.txtRxUnit.tag = RXunit; 
+0

#define SQ 1 #define POB 2 #define RXunit 3 – user3360057

+0

你是否設置了委託... pls顯示你的代碼cellForRowAtIndex –

+0

yes alredy set委託cell.txtPOB.delegate = self; cell.txtRxUnit.delegate = self; cell.txtSQ.delegate = self; – user3360057