2011-02-16 74 views
1

我的iPhone應用程序中有一個奇怪的問題。 我創建了一個UITableView,包含4個部分和3個行,共計12行。但是另一個UITableView問題

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

上述方法只被調用了9次而不是12次。爲什麼會發生這種情況。 我的第四部分沒有被修建,但我的第一部分被複製爲第四部分。

感謝您的時間和幫助。

普萊舍參考下

@interface MainViewController : UITableViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> 
{ 
} 
@end 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    CGRect frameRect = CGRectMake(0,0,320,460); 

    UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect 
    style:UITableViewStyleGrouped]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    tableView.backgroundColor = [UIColor purpleColor]; 
    tableView.scrollEnabled = YES; 

    self.view = tableView; 
    [tableView release]; 

    [super viewDidLoad]; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 3; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 4; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"CELL IS NIL %i", indexPath.section); 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 


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

     if (indexPath.section == 0) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Tmail"; 

       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"English"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
      else 
      { 
       cell.text = @"Hindi"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
     } 
     else if (indexPath.section == 1) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Street"; 

       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"City"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
      else 
      { 
       cell.text = @"State"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
     } 
     else if (indexPath.section == 2) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Salem"; 

       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"Samalpatti"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
      else 
      { 
       cell.text = @"Chennai"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
     } 

     else if (indexPath.section == 3) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"NOKIA"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"SAMSUNG"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 

      } 
      else 
      { 
       cell.text = @"SONY"; 
       UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 
       aField.placeholder = @"Mandatory"; 
       aField.delegate = self; 
       aField.textColor = [UIColor blackColor]; 
       [cell addSubview:aField]; 
       [aField release]; 
      } 
     } 



    } 

    return cell; 
} 

enter image description here

+0

嘗試清潔構建。並從項目文件夾中刪除構建目錄。我遇到了同樣的問題,甚至更糟。所以刪除構建目錄 – Robin 2011-02-16 05:02:18

+0

@ barbaghal,通過圖像顯示你需要調整你的文本框架和標籤框架之間的差距。 – Ishu 2011-02-17 04:14:14

回答

2

其實我的代碼,當你滾動那麼其他行的情況下,創造了你有更多的行,因此認爲你需要編寫的代碼是這樣,

編輯一個

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSLog(@"CELL IS NIL %i", indexPath.section); 

     static NSString *CellIdentifier = @"Cell"; 
if (cell == nil) 
    { 

     CGRect CellFrame = CGRectMake(0, 0, 300, 70); 
     cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease]; 
     CGRect labLabelFrame = CGRectMake(5, 5, 250, 40); 
     UILabel *lblTemp; 

     lblTemp = [[UILabel alloc] initWithFrame:labLabelFrame]; 
     lblTemp.tag = 1; 
     [cell.contentView addSubview:lblTemp]; 
     [lblTemp release]; 

     CGRect textFieldFrame= CGRectMake(200, 5, 120, 30); 

     UITextField *txt=[[UITextField alloc] initWithFrame:textFieldFrame]; 
     text.tag=2; 
     [cell.contentView addSubview:txt]; 
     [txt release]; 

    } 
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; 
     UITextField *aField=(UITextField *)[cell viewWithTag:2]; 

    [email protected]""; 
    [email protected]""; 
if (indexPath.section == 0) 
       { 
        if(indexPath.row == 0) 
        { 
         cellLabel.text = @"Tmail"; 


         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
        else if (indexPath.row == 1) 
        { 
         cellLabel.text = @"English"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
        else 
        { 
         cellLabel.text = @"Hindi"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
       } 
       else if (indexPath.section == 1) 
       { 
        if(indexPath.row == 0) 
        { 
         cellLabel.text = @"Street"; 


         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
        else if (indexPath.row == 1) 
        { 
         cellLabel.text = @"City"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
        else 
        { 
         cellLabel.text = @"State"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
       } 
       else if (indexPath.section == 2) 
       { 
        if(indexPath.row == 0) 
        { 
         cellLabel.text = @"Salem"; 
         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
        else if (indexPath.row == 1) 
        { 
         cellLabel.text = @"Samalpatti"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
        else 
        { 
         cellLabel.text = @"Chennai"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
       } 

       else if (indexPath.section == 3) 
       { 
        if(indexPath.row == 0) 
        { 
         cellLabel.text = @"NOKIA"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
        else if (indexPath.row == 1) 
        { 
         cellLabel.text = @"SAMSUNG"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 


        } 
        else 
        { 
         cellLabel.text = @"SONY"; 

         aField.placeholder = @"Mandatory"; 
         aField.delegate = self; 
         aField.textColor = [UIColor blackColor]; 

        } 
       } 


      return cell; 
     } 
+0

@Ishu:謝謝你的回覆dude ..它的作品,但在uitextfiled裏面輸入文本後,當滾動可用視圖我得到文本重疊問題..任何想法? – user198725878 2011-02-16 05:17:48

1

問題與您的代碼是:

1)單元格正在重用。但文本字段你添加爲子視圖不會被刪除!

2)每當您的cellforRowAtIndex被調用時,您每次都添加文本字段。但是你忘了你已經添加了一個文本框,但沒有刪除它。

  • 這就是重疊的原因。

解決方案: 子類UITableViewCell並在其initWithFrame方法中添加文本字段並將數據傳遞到該單元格。這將解決你的問題,這也會減少你的cellForRowAtIndex方法的長度。

示例代碼: CustomCell.h:

#import <UIKit/UIKit.h> 

@class contactr; 
@interface CustomTableCell : UITableViewCell 
{ 
    UITextField *mTextField; 
} 
@property(nonatomic , retain) UITextField *textField; 

@end 

CustomCell.m:

#import "CustomTableCell.h" 
@synthesize textField = mTextField; 
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    {  
     UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)]; 

     self.textField = textField; 
     [textField release]; 
     [self addSubview:self.textField]; 

    } 
    return self; 
} 

- (void)dealloc 
{ 
    self.textfield = nil; 
    [super dealloc]; 
} 


@end 

使用這個類作爲您tableViewCell。

而且你cellForRowAtIndex必須是:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"CELL IS NIL %i", indexPath.section); 

    static NSString *CellIdentifier = @"Cell"; 
    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 


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

    } 
cell.textField.placeholder = @""; 
cell.textField.text = @""; 
     if (indexPath.section == 0) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Tmail"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"English"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
      else 
      { 
       cell.text = @"Hindi"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
     } 
     else if (indexPath.section == 1) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Street"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"City"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
      else 
      { 
       cell.text = @"State"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 

      } 
     } 
     else if (indexPath.section == 2) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"Salem"; 

       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"Samalpatti"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
       cell.textField.textColor = [UIColor blackColor]; 

      } 
      else 
      { 
       cell.text = @"Chennai"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
       cell.textField.textColor = [UIColor blackColor]; 

      } 
     } 

     else if (indexPath.section == 3) 
     { 
      if(indexPath.row == 0) 
      { 
       cell.text = @"NOKIA"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
       cell.textField.textColor = [UIColor blackColor]; 
      } 
      else if (indexPath.row == 1) 
      { 
       cell.text = @"SAMSUNG"; 
       cell.textField.placeholder = @"Mandatory"; 
       cell.textField.delegate = self; 
       cell.textField.textColor = [UIColor blackColor]; 
      } 
      else 
      { 
       cell.text = @"SONY"; 
       cell.textField.placeholder = @"Mandatory"; 
      cell.textField.delegate = self; 
       cell.textField.textColor = [UIColor blackColor]; 
      } 
     } 




    return cell; 
} 
0

從您最初的代碼中,實現代碼如下行爲正常。
您可以爲前3個部分創建單元格,因爲它們是可見的,並且表格視圖中沒有可以重複使用的額外單元格。
但是,無論何時滾動到第4部分(開始時都不可見),您都會重新使用單元格(恰好來自第一部分的單元格不在屏幕之外)。

爲了不丟失用戶在每個單元格中輸入的數據,您需要將每個UITextField(或至少它的文本)存儲在單獨的數組中。當表格需要某個單元格時,您可以查找數組中的文本字段。如果不存在,請創建並添加一個新的UITextField。 PS:我希望代碼不會有任何錯別字。我在工作,我沒有在這裏的Mac。



@interface MainViewController : UITableViewController 
{ 
NSMutableArray* textFields; 
} 
@end 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    CGRect frameRect = CGRectMake(0,0,320,460); 

    UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect 
    style:UITableViewStyleGrouped]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    tableView.backgroundColor = [UIColor purpleColor]; 
    tableView.scrollEnabled = YES; 

    self.view = tableView; 
    [tableView release]; 

    [super viewDidLoad]; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 3; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 4; 
} 

- (UITextField*)getTextFieldAtIndexPath:(NSIndexPath*)indexPath 
{ 
    if (textFields == nil) 
     textFields = [[NSMutableArray alloc] init]; 

    // Add padding, just in case UITableView doesn't need the cells in order 
    while (indexPath.section >= [textFields count]) 
    { 
     [textFields addObject:[NSNull null]]; 
    } 

    // Get the section list 
    NSMutableArray* sectionList = [textFields objectAtIndex:indexPath.section]; 

    if (sectionList == [NSNull null]) 
    { 
    sectionList = [[[NSMutableArray alloc] init] autorelease]; 
    [textFields replaceObjectAtIndex:indexPath.section withObject:sectionList]; 
    } 


    // Now that we have the section list, we need to do the same for the row 
    while (indexPath.row >= [sectionList count]) 
    { 
     [sectionList addObject:[NSNull null]]; 
    } 

    UITextField* afield = [sectionList objectAtIndex:indexPath.row]; 

    if (afield == [NSNull null]) 
    { 
     // If there was no afield stored there, just create a new one 
     afield = [[UITextField alloc] initWithFrame:CGRectMake(100,10,200,40)]; 
     afield.placeholder = @"Mandatory"; 
     afield.delegate = self; 
     afield.textColor = [UIColor blackColor]; 

     [sectionList replaceObjectAtIndex:indexPath.row withObject:aField]; 

     [afield release]; 
    } 

    return afield; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"CELL IS NIL %i", indexPath.section); 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UITextField *aField; 

// If we can't reuse the cell, just create a new one 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    else 
    { 
     // if we are reusing the cell, just remove the old textField to make room for the new one 
     [[cell.contentView viewForTag:1234] removeFromSuperview]; 
    } 

    // Get the text field for this cell. The method below will create a new one if it doesn't exist, or just return the existing one if found. 
    aField = [self getTextFieldAtIndexPath:indexPath]; 

    // Just add this textfield to the cell 
    [cell.contentView addSubView:aField]; 

    // Now that we added the textfield, we only need to set the title of the cell. The content of the textField will be cached and moved around. 

     if (indexPath.section == 0) 
     { 
      if(indexPath.row == 0) 
       cell.text = @"Tmail"; 
      else if (indexPath.row == 1) 
       cell.text = @"English"; 
      else 
       cell.text = @"Hindi"; 
     } 
     else if (indexPath.section == 1) 
     { 
      if(indexPath.row == 0) 
       cell.text = @"Street"; 
      else if (indexPath.row == 1) 
       cell.text = @"City"; 
      else 
       cell.text = @"State"; 
     } 
     else if (indexPath.section == 2) 
     { 
      if(indexPath.row == 0) 
       cell.text = @"Salem"; 
      else if (indexPath.row == 1) 
       cell.text = @"Samalpatti"; 
      else 
       cell.text = @"Chennai"; 
     } 

     else if (indexPath.section == 3) 
     { 
      if(indexPath.row == 0) 
       cell.text = @"NOKIA"; 
      else if (indexPath.row == 1) 
       cell.text = @"SAMSUNG"; 
      else 
       cell.text = @"SONY"; 
     } 
    } 

    return cell; 
}