2011-04-13 73 views
1

我有一個分段的tableView加載所有部分的所有單元格中的所有數據。 每個單元格中都有一個textField。UITableView單元格/數據消失

桌面視圖並不完全適合iPad屏幕,我無法訪問所有不可見的單元格以讀取/保存數據。當我在「textField」中進行更改時,然後向上滾動,向下滾動,所有更改都消失。

我需要加載所有單元格,甚至不可見一次,以便能夠訪問它們。

對不起,我剛剛在幾天前開始使用表格...... 我認爲這個問題與可重用單元格有關,但不知道如何解決它。

尋求你的幫助,請。

初始化:

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

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


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ; 
     textField.enabled = NO; 
     cell.accessoryView = textField; 
     [textField release]; 

} 
    UITextField *textField = (UITextField*)cell.accessoryView; 

    if(indexPath.section == 0) 
     cell.textLabel.text = [idenInfo objectAtIndex:indexPath.row]; 
    else if (indexPath.section == 1) 
     cell.textLabel.text = [prodInfo objectAtIndex:indexPath.row]; 
    else if (indexPath.section == 2) 
     cell.textLabel.text = [visInfo objectAtIndex:indexPath.row]; 


    if(indexPath.section == 0) 
     textField.text = [idenInfoRez objectAtIndex:indexPath.row]; 
    else if (indexPath.section == 1) 
     textField.text = [prodInfoRez objectAtIndex:indexPath.row]; 
    else if (indexPath.section == 2) 
     textField.text = [visInfoRez objectAtIndex:indexPath.row]; 

    textField = nil; 

    return cell; 
} 

回答

3

首先:你沒有加載所有細胞,包括無形的。這就是UITableView和MVC模式的重點:將您的視圖與數據分開。

當用戶更改textField中的值時,您需要執行的操作是更新您的數據源(即您的情況爲idenInfoRezprodInfoRezvizInfoRez)。所以你必須設置你的UIViewController作爲每個文本字段的代理,並在用戶輸入時更新值。

+0

我如何知道哪個單元格(indexPath)的textField稱爲事件? 所以我實際上可以修改相應的數據源? – Razp 2011-04-13 11:58:49

+1

看看UITableViewDataSource協議方法。你會發現你需要實現的方法,以便知道表中的用戶操作修改了哪個indexPath。我也會做一些閱讀。 iOS應用程序編程指南介紹了MVC設計模式。表格視圖控制器編程指南是表格的最佳開始。 – XJones 2011-04-13 20:44:33

-1

你說得對,問題是單元格會被重用。有兩種辦法可以解決這個問題,快速和骯髒的人會不使用可重複使用的細胞:

刪除此:

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


if (cell == nil) { 

而剛剛離開這個:

UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ; 
    textField.enabled = NO; 
    cell.accessoryView = textField; 
    [textField release]; 

這應該是好的,如果你的tableview中只有少量的單元格(大約少於50個)。


更好的解決方案是將細胞重新使用,並填寫他們的文本區域,因爲他們被要求。該方法因應用程序而異,但您基本上不會直接訪問單元格,並將單元格的數據存儲在其他位置,例如,一個NSStrings的NSArray。然後你可以操縱NSArray。你的cellForRowAtIndexPath方法會是這個樣子:

textField.text = [arrData objectAtIndex:indexPath.row]; 
+0

如果我刪除 static NSString * CellIdentifier = @「Cell」; 我無法初始化您的代碼。由於CellIdentifiel不存在。 如果我保留它,但刪除「如果」,問題沒有解決。 (我只有13個單元格) – Razp 2011-04-13 12:08:07

+0

只需傳遞'nil'作爲參數即可。請注意,雖然這樣做會使你的表格視圖效率非常低,如果它後面包含比當前13更多的行。 – jlehr 2011-04-13 13:07:09

+0

仍然無法訪問單元格,並且數據在滾動後仍然會重新加載。 – Razp 2011-04-13 13:23:13

0

你可以做的是這樣的:在每個TextField店的價值包含在文本字段的編輯更改事件在NSMutableArray其數量等於數量細胞。即

-(IBAction) EditingChanged: (id) sender 

{ 

    UITextField *txt =(UITextField*)sender; 

    NSString* str =[[NSString alloc] initWithString: txt.text]; 
    //get current text string 

    NSInteger path =[tableView indexPathForSelectedRow].row; 
      // get currently selected row, this could be a bit different depending on the number of sections 

    [yourMutableArray insertObject: str atIndex: path]; 

    [str release] 

} 

然後,您可以填充TextFieldNSMutableArray隨時細胞被重新即

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

{ 

    ...... // create the cells here and use viewTag to get the textFields 

    textField.text= [yourMutableArray objectAtIndex:indexPath.row]; 

    //This may be a bit different depending on the number of sections. 

} 

同樣的值,請注意,這可能是最好yourMutable數組初始化的能力細胞數量。

如果代碼格式不正確,我很抱歉,因爲這是我在stackoverflow上的第一篇文章 - 也可能在代碼中存在一些拼寫錯誤。希望這可以幫助某人。

1

[UIView beginAnimations:@「ShiftUp」context:nil]; [UIView setAnimationDuration:0.0001]; (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger section = [indexPath section];

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

如果(細胞==零){

[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = objCustCell; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone ; 

}

if (section == 0) { 

     switch (indexPath.row) { 
      case 0:{ 

      cell.lable.text = @"Date of Birth"; 
      cell.field.placeholder = @"Birth Name"; 

       break; 
      } 

      case 1:{ 

       cell.lable.text = @"Enter Your Name"; 
       cell.field.placeholder = @"Full Name"; 

       break; 
      } 


      default: 
       break; 
     } 

} 

[UIView beginAnimations:@"ShiftUp" context:nil]; 
[UIView setAnimationDuration:0.0001]; 

// [UIView beginAnimations: @"ShiftUp" context:nil]; 

NSLog(@"Load cellfor raw at index "); 
// Configure the cell. 
     return cell; 

}

注:UIView的動畫不會讓文本字段搬走數據或任何UIcontroller將保持原來的狀態相同! 不要提交動畫,否則它不會工作!

0

每次我們小區分配到不同的數據時,數據不會重裝電池,每一個數據覆蓋以前的數據時,才分配細胞清除細胞, 像小區=零

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

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

      cell=nil; 
     //it clear data in the cell 
      if (cell == nil) 
{ 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
       UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ; 
       textField.enabled = NO; 
       cell.accessoryView = textField; 
       [textField release]; 

     }