2013-04-09 24 views
0

我試圖從包含在目錄的iOS的NSMutableArray堅持

//in my header 
@property (strong, nonatomic) NSMutableArray *files; 
//in my tableView:cellForRow:atIndexPath: 
static NSString *cellid = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; 
    cell.textLabel.text = [_files objectAtIndex:indexPath.row]; 
} 
return cell; 

文件(_files設置等於[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self downloadsDir];之前被調用此方法)這工作數組填充一個UITableView,並顯示正確的文件。 問題是如果我將文件添加到目錄中,然後使用tableView reloadData,則會添加一個新文件,但標題將與另一個文件重複。例如

添加文件othertest.txt

++++++++++++++++++++++++++ 
text.txt 
++++++++++++++++++++++++++ 
testing.txt 
++++++++++++++++++++++++++ 
testing.txt 
++++++++++++++++++++++++++ 

++++++++++++++++++++++++++ 
text.txt 
++++++++++++++++++++++++++ 
testing.txt 
++++++++++++++++++++++++++ 

表視圖中添加文件

前表視圖應該

++++++++++++++++++++++++++ 
text.txt 
++++++++++++++++++++++++++ 
testing.txt 
++++++++++++++++++++++++++ 
othertest.txt 
++++++++++++++++++++++++++ 

,如果我重新啓動應用程序,它會顯示正確的文件。但由於某種原因,它是這樣做的。任何人都知道什麼可能是錯的?

回答

3
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; 
    cell.textLabel.text = [_files objectAtIndex:indexPath.row]; 
} 
return cell; 

除了分配新單元格之外,您沒有設置單元格標籤文本。試試這個:

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; 
} 
cell.textLabel.text = [_files objectAtIndex:indexPath.row]; 
return cell; 

相同的代碼,但是我感動,你這樣,當你重複使用的單元,以及當你創建一個新的,因爲它得到執行設置單元格的文本行。

+0

謝謝,這工作。當他們讓我時,我會標記它是正確的。 – 2013-04-09 03:11:58

+0

非常普遍的問題。樂意效勞。 – Caleb 2013-04-09 03:12:58