2011-08-17 34 views
0

嗨,我有一個表格視圖,可以添加和刪除單元格。但是當我刪除自帶的應用程序的唯一細胞,我嘗試添加一個新的細胞與「梅根道」的文字,只是自動文本改變它自身的原始細胞的文字是「樓1」在這裏我的代碼!UITableView的BUG,增加細胞犯規插入文本所需

- (IBAction)saveButton:(id)sender { 
FacePlatesViewController * viewController = (FacePlatesViewController 
*)self.parentViewController; 

[viewController addObject:[NSMutableDictionary dictionaryWithObject:text.text  
forKey:@"name"]]; 

[self dismissModalViewControllerAnimated:YES]; 
} 

- (IBAction)cancel:(id)sender { 
[self dismissModalViewControllerAnimated:YES]; 
} 



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

static NSString *CellIdentifier = @"Cell"; 

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
reuseIdentifier:CellIdentifier] autorelease]; 
    cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"]; 
UIImage * myimage = [UIImage imageNamed: @"check.png"]; 
image = [[UIImageView alloc]initWithImage:myimage]; 



tableView.backgroundColor = [UIColor clearColor]; 


    self.myTableView = tableView; 


} 
return cell; 

} 

這是保存按鈕!任何幫助將是非常讚賞:d感謝

+1

我們需要FacePlatesViewController的代碼才能幫助您解決這個問題。 –

+0

我真的懷疑這是一個UITableView的錯誤,正如標題所述。 – EmilioPelaez

回答

1

你的代碼是相當混亂。我在下面對它進行了改進,並添加了以下評論:

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

    static NSString *CellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
    reuseIdentifier:CellIdentifier] autorelease]; 
    } //The if block should end here. You should set the cell's label irrespective whether the cell was nil. This is the cause of the issue you are facing. 

    cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"]; 

    //You are doing nothing with the UIImage 
    //UIImage * myimage = [UIImage imageNamed: @"check.png"]; 
    //image = [[UIImageView alloc]initWithImage:myimage]; 


    //You should be setting the background color just once, in ViewDidLoad, not here. 
    //tableView.backgroundColor = [UIColor clearColor]; 

    //I don't see why this is required 
    //self.myTableView = tableView; 

    return cell; 

} 
+0

當它完美的工作!!!!! :D非常感謝akshay:D我真的很感激它! – PengOne2

+0

嘿人很抱歉打擾你,但你的代碼就像一個魅力,非常感謝! ,但是這個錯誤現在出現在另一個地方:O在我添加單元格之後,文本是好的,但是當我切換頁面然後返回時,單元格返回到房子1文本。你知道這是爲什麼嗎?是否因爲我必須在離開頁面之前保存應用程序的狀態?非常感謝:D – PengOne2

+0

您是否確認單元格數組具有正確的值? – Akshay

0

你存儲這些細胞的文字標籤在任何類型的數組或字典,並在那裏使用標準裝載他們「的cellForRowAtIndexPath?」如果是這樣,那麼您需要刪除該數據源中的字典/數組條目,以防止再次使用它。

我不能肯定地告訴沒有看到更多的代碼...但它聽起來像一個電池的回收問題。我可能是錯的......但我需要看到更多的信息。

+0

是的,我是我在這裏做了一個plist ..生病更新與cellForRow的問題。並感謝您的幫助:ð哦,問題是與刪除單元格與文本。該應用程序從1個名爲「House 1」的單元開始,當用戶刪除該單元並添加一個新單元時,它們被帶到另一頁以輸入新單元的文本。當他們點擊保存按鈕,文本不是他們鍵入的內容仍然是「衆議院1」 – PengOne2