2013-09-01 16 views
2

我試圖實現與單元格和文本字段的UITableView,但單元格保持重疊並且文本字段從一個部分跳轉到另一個部分。在表視圖中重疊的單元格

有人可以檢查我的代碼是否有錯誤嗎?

listForItemsinfo =[[NSArray alloc]initWithObjects:@"Name",@"Price", nil]; 
listForBrandYear =[[NSArray alloc] initWithObjects:@"Year",@"Alfa",@"aston",@"audi",@"bentley",@"BMW",@"Cadillac",@"Chevrolet",@"Ferrari",@"Ford",@"Jaguar",@"Jeep",@"Kia",@"Landrover",@"Mazda",@"Mercedes",@"Mitsubishi",@"Nissan",@"Porshe",@"Subaru", nil]; 



listForkeywords =[[NSArray alloc] initWithObjects:@"KeyWord1",@"Keyword2", nil]; 
listForcarType =[[NSArray alloc] initWithObjects:@"Sedan",@"Convertible",@"Coupe",@"Heatchback", nil]; 
listForItemsCategory =[[NSArray alloc] initWithObjects:@"cat1",@"cat2",@"cat3",@"cat4",@"cat5",@"cat6", nil]; 

sections = [[NSArray alloc]initWithObjects:@"Item's info",@"Brand, Year",@"Keywords",@"Car Type",@"Category", nil]; 
listOfsections =[[NSArray alloc]initWithObjects:listForItemsinfo,listForBrandYear,listForkeywords,listForcarType,listForItemsCategory, nil]; 



- (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]; 


    if (indexPath.section == 0) { 

      // Add a UITextField 
     UITextField *textField = [[UITextField alloc] init]; 
     // Set a unique tag on each text field 
     textField.tag = 1+ indexPath.row; 
     // Add general UITextAttributes if necessary 
     textField.enablesReturnKeyAutomatically = YES; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     [cell.contentView addSubview:textField]; 


    }else if (indexPath.section == 1){ 
     if (indexPath.row==0) { 

     UITextField *textField = [[UITextField alloc] init]; 
     // Set a unique tag on each text field 
     textField.tag = 2+ indexPath.row; 
     // Add general UITextAttributes if necessary 
     textField.enablesReturnKeyAutomatically = YES; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
     [cell.contentView addSubview:textField]; 
     } 
    }else if (indexPath.section == 2){ 
     if (indexPath.row==0 | indexPath.row ==1) { 

      UITextField *textField = [[UITextField alloc] init]; 
      // Set a unique tag on each text field 
      textField.tag = 3+ indexPath.row; 
      // Add general UITextAttributes if necessary 
      textField.enablesReturnKeyAutomatically = YES; 
      textField.autocorrectionType = UITextAutocorrectionTypeNo; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      [cell.contentView addSubview:textField]; 
     }else{ 

     } 
    } 
} 
//[email protected]"wqewq"; 
//cell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

[self configureCell:cell atIndexPath:indexPath]; 



return cell; 
} 

- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath { 

if (indexPath.section == 0) { 

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:1+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    theCell.textLabel.text = NULL; 


    // Configure UITextAttributes for each field 
    if(indexPath.section == 0 & indexPath.row == 0) { 
     textField.placeholder = @"Name"; 
     textField.returnKeyType = UIReturnKeyNext; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
    } else if(indexPath.section == 0 & indexPath.row == 1) { 
     textField.placeholder = @"Price"; 
     textField.returnKeyType = UIReturnKeyNext; 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
    }else if (indexPath.section ==0 & indexPath.row >1){ 
     [textField removeFromSuperview]; 
    } 
}else if (indexPath.section == 1) { 

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:2+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    // Configure UITextAttributes for each field 
    if(indexPath.section == 1 & indexPath.row == 0) { 
     textField.placeholder = @"Year"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
     theCell.textLabel.text = NULL; 
    }else if(indexPath.section == 1 & indexPath.row != 0){ 
     [textField removeFromSuperview]; 
     theCell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

    } 
}else if (indexPath.section == 2) { 

    // Get the text field using the tag 
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:3+indexPath.row]; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = theCell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    // Configure UITextAttributes for each field 
    if(indexPath.section == 2 & indexPath.row == 0) { 
     textField.placeholder = @"Keyword1"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
    }else if(indexPath.section == 2 & indexPath.row == 1) { 
     textField.placeholder = @"Keyword2"; 
     textField.returnKeyType =UIReturnKeyDone; 
     textField.autocapitalizationType = UIKeyboardTypeNumberPad; 
    }else{} 
} 



} 

固定!如果任何人有同樣的問題(工作代碼)

static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Add a UITextField 
    UITextField *textField = [[UITextField alloc] init]; 
    // Set a unique tag on each text field 
    textField.tag = 1 + indexPath.row; 
    textField.delegate=self; 
    // Add general UITextAttributes if necessary 
    textField.enablesReturnKeyAutomatically = YES; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    // Position the text field within the cell bounds 
    CGRect cellBounds = cell.bounds; 
    CGFloat textFieldBorder = 10.f; 
    // Don't align the field exactly in the vertical middle, as the text 
    // is not actually in the middle of the field. 
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

    textField.frame = aRect; 

    textField.placeholder = @"Search for an item"; 
    textField.returnKeyType = UIReturnKeyDefault; 
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 

     if ((indexPath.section == 0) & (indexPath.row==0)) { 

      [cell.contentView addSubview:textField]; 
      cell.textLabel.text=NULL; 
     }else{ 
      [textField removeFromSuperview]; 
      cell.textLabel.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
     } 
+0

請檢查下面的鏈接,希望,它會爲你工作:-) http://stackoverflow.com/a/23734829/1999368 –

回答

0

表不會使細胞重疊,但細胞得到重用。你的問題是你初始化時只能配置單元。實際上,無論是從-dequeueReusableCellWithIdentifier:創建新單元還是獲取舊單元,都需要執行該配置。

也許你認爲表格視圖保存你給它的每個單元格?這不是它的工作原理......可重複使用的單元格意味着一張表格只需要一次可以顯示的單元格數量。在您滾動視圖時,滾動頂部或底部的單元格將作爲新內容的新單元格進行回收。但是,您並未設置新內容,因此舊內容會顯示。

添加關閉支架您if的第一行後,使之始終執行的代碼的其餘部分:

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

if (indexPath.section == 0) { 
+0

謝謝你的工作 – George

相關問題