2013-03-18 86 views
1

我試圖在表格視圖中填充單元格(我有兩個自定義類型的單元格,其中包含使用標識符「info_cell」和「person_cell」創建的不同元素,在UITableView之上的分段控制上,我決定加載[tableView reload])。當我嘗試訪問單元格內的UILabels時,我得到的標籤爲空。UITableView中單元格內的UILabel始終爲空(兩個單元格原型)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = (viewType == INFO_VIEW) ? @"info_cell" :@"person_cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if(viewType == INFO_VIEW){ 
     NSLog(@"INFO = %@", info_text_some_string); 
     UILabel *lblInfo = (UILabel *)[cell viewWithTag:200]; 
     [lblInfo setText:info_text_some_string]; 
    } 
    else{ 
     // there is part for person 
    } 
    return cell; 
} 

相同的代碼工作時,我只有一個原型單元格內的表(UITableView是UIVewController內)。可以採取什麼問題就在這裏,我已經檢查100次:小區標識符都行,標籤標記爲200

這是UISegmentControl

- (IBAction)changeView:(id)sender { 
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender; 
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex; 

    if (selectedSegment == 0) { 
     viewType = INFO_VIEW; 
    } 
    else{ 
     viewType = PERSON_VIEW; 
    } 
    [tableView reloadData]; 
} 

我已經加入和必要的方法行動的tableView並連接代表我數據源。 有沒有人有任何想法爲什麼它爲空?

+0

您可以發佈它爲你工作的解決方案,因爲我面臨類似的問題.. – Shivaay 2014-06-20 21:36:08

回答

-1

爲什麼不做這樣的事情?

[[cell textLabel] setText: @"text goes here"]; 

並跳過UILabel部分?

+0

我現在增加了,它是自定義類型的單元格上有不同的元素 – 2013-03-18 21:54:37

0

試試這個通常我按照這個過程,每當我與自定義單元格去CellRorRowAtIndexPath

if(!cell) 
    { 
     cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
     //customcell is your UITableViewCell created by you in ur xib 
     NSData *archivedData =[NSKeyedArchiver archivedDataWithRootObject:customcell]; 
     cell =[NSKeyedUnarchiver unarchiveObjectWithData:archivedData]; 

    } 
    if(viewType ==INFO_VIEW) 
    { 
    [(UILabel *)[cell viewWithTag:200] setText:@"you text"]; 
    } 
    else{ 
     // person view.... 
     } 

這樣你收集你的你的所有元素,併爲其設定值。分享你的效果好嗎

0

假設你有正確繼承你的UITableViewCells(我用InfoCell和PersonCell爲例),你可以試試這個:

if(viewType == INFO_VIEW) 
{ 
    InfoCell *cell = (InfoCell *)[tableView dequeueReusableCellWithIdentifier:@"info_cell"]; 
    // do your stuff for info here 
} 
else if(viewType == PERSON_VIEW) 
{ 
    PersonCell *cell = (PersonCell *)[tableView dequeueReusableCellWithIdentifier:@"person_cell"]; 
    // do your stuff for person here 
} 
相關問題