2013-04-09 96 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