2012-04-23 64 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