2012-04-23 62 views
0

当我再次向下滚动时,tableView中的文本将消失。以编程方式填充的不刷新屏幕TableView

enter image description hereenter image description here

而且我的代码是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [screenDefBuild.elementsToTableView count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 

    ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:indexPath.row]; 
    cell.textLabel.text = currentScreenElement.objectName; 

    currentRow++;  
    return cell; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    [tableView setDataSource:self]; 
    [self.view addSubview:tableView]; 
} 

我也想填补我的表视图,以整个屏幕。 (顶部灰色表带)。

+0

试试这个隐藏这段代码// if(cel == nil)....然后现在滚动 – akk 2012-04-23 08:11:10

+2

这两个注释都是非常糟糕的想法,并且破坏了表视图的几个系统优化。 – jackslash 2012-04-23 08:20:07

+0

要解决灰色背带问题,请将您的桌面背景颜色设为白色 – Charan 2012-04-23 08:47:02

回答

2

我不知道你有这个变量

currentRow++; 

但是,不管你使用了,我打赌它打破你的代码做什么。

UITableView将在每次单元即将出现在屏幕上时调用cellForRowAtIndexPath,而不管屏幕是否在屏幕上。当你向下滚动然后向上滚动时,这个变量会增加超出你的数据范围,因此你会得到空单元格。

您需要设计此方法,以便可以随时在表视图中创建任何单元格。你不能依靠单元格的制作顺序,而滚动你将不得不一次又一次地制作同一个单元格。只能使用indexPath来确定您目前应该制作的单元格。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html

+0

我不再使用它了。这不再存在,但问题仍然存在。 – Kuba 2012-04-23 08:31:01

+2

然后,您必须查看'[screenDefBuild.elementsToTableView objectAtIndex:indexPath.row]'或'currentScreenElement.objectName',因为它们在第二次由于某种原因被调用时返回nil。 – jackslash 2012-04-23 08:32:38

+0

谢谢,好吧,我在上面。如果我想出来,我会回复。 – Kuba 2012-04-23 08:35:07

0

回答为将以下问题灰色条带的第二部分。您正在将表格视图添加到当前视图,因此您应该使用self.view.frame的大小属性,但不是原点。您希望将其设置为0,0。

变化

tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 

CGRect viewFrame=self.view.frame; 
viewFrame.origin=CGPointZero; 
tableView = [[UITableView alloc] initWithFrame:viewFrame]; 

至于你question-的第一部分,这是奇怪的,你似乎做正确的一切。我可能会建议的一件事是在viewDidLoad的末尾添加[tableView reloadData];

+2

事实并非如此。根据定义,视图的边界具有“CGPointZero”的起点(除非您更改它)。边界是视图内部空间的自身坐标。在'viewDidLoad'的末尾添加'reloadData'并不会像'tableView'被填充一样帮助最初但在滚动后失败。这里不再调用viewDidLoad。 – jackslash 2012-04-23 08:53:51

+0

你是对的,我知道它与view.frame混淆。至于'viewDidLoad',我知道它并不是在那里调用的,那就像一个盲目的镜头 – 2012-04-23 09:00:21