2011-10-14 160 views
3

我有兩個tableviews沒有顯示,一個有幾個的tableview單元的各單元打開相同的子視圖,但新的數據initalized ..上的UITableViewCell附件蜱在某些情況下

大約有100 - 200項到表我有一個附件視圖,這是一個勾選,當選中一個單元格時,它會勾選單元格,然後再次加載主視圖。

如果我選擇相同的單元格以獲取相同的數據集,它將在屏幕中間加載先前選定的單元格(因此它知道它的索引路徑),但是「取決於列表中的深度」將打勾或將不會被看到..

它傾向於在表格的前30/40%左右工作,但任何較低的刻度將不可見...這是除非我來回來越來越深入每個時間有時候我可以讓滴答出現在tableview的更深處。有誰知道爲什麼會發生這種情況嗎?

有沒有人有過這種事情發生在他們之前?

在進一步的調查,我認爲事情是這個方法裏面去錯了..

首先,在子視圖一旦用戶選擇了一個小區這種方法被稱爲

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// Navigation logic may go here. Create and push another view controller. 
    [self.navigationController popViewControllerAnimated:YES]; //pops current view from the navigatoin stack 

    //accesses selected cells content 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // now you can use cell.textLabel.text 

    //This if statment is based off which cell was selected in the parent view so that it knows which cell to pass the data back to 
    if (parentViewSelectedIndexPath.section == 0) { 
     if (parentViewSelectedIndexPath.row == 0) { 
      manufactureCellTextLabel = cell.textLabel.text; //passing label text over to NSString for use with delegate (check "viewwilldissapear") 
      [[self delegate] setManufactureSearchFields:manufactureCellTextLabel withIndexPath:indexPath]; //This is where I pass the value back to the mainview 
     } 
// a few more If statements for the other methods I can pass data too. 


//--- this if block allows only one cell selection at a time 
    if (oldCheckedData == nil) { // No selection made yet 
     oldCheckedData = indexPath; 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

    } 
    else { 
     UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldCheckedData]; // finding the already selected cell 
     [formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone]; 

     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell 
     oldCheckedData = indexPath; 
    } 
} 

這通過索引路徑到主視圖的方法...

- (void) setManufactureSearchFields:(NSString *)cellLabeltext withIndexPath:(NSIndexPath *)myIndexPath 
    { 
     manufactureSearchObjectString = cellLabeltext; 
     manufactureResultIndexPath = myIndexPath; 
     [self.tableView reloadData]; //reloads the tabels so you can see the value. 
    } 

//然後設置一個在未來的方法所使用的manufactureResultIndexPath傳回給子視圖

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    //--- Idendify selected indexPath (section/row) 
    if (indexPath.section == 0) { 
     //--- Get the subview ready for use 
     VehicleResultViewController *vehicleResultViewController = [[VehicleResultViewController alloc] initWithNibName:@"VehicleResultViewController" bundle:nil]; 
     // ... 
     //--- Sets the back button for the new view that loads 
     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease]; 

     // Pass the selected object to the new view controller. 
     [self.navigationController pushViewController:vehicleResultViewController animated:YES]; 

     [vehicleResultViewController setDelegate:self]; 

     if (indexPath.row == 0) 
     { 
      vehicleResultViewController.title = @"Manufacture"; 
      [vehicleResultViewController setRequestString:@"ID.xml"]; //sets the request string in searchResultsViewController 
      vehicleResultViewController.dataSetToParse = @"ID"; // This is used to controll what data is shown on subview... logic 
      [vehicleResultViewController setAccessoryIndexPath:manufactureResultIndexPath]; //sends indexpath back to subview for accessory tick 
      vehicleResultViewController.parentViewSelectedIndexPath = indexPath; 
     } 


//etc etc 
} 

而且finaly我把它傳遞給我的子視圖是經過indexpath到oldCheckedData

方法
- (void)setAccessoryIndexPath:(NSIndexPath *)myLastIndexPath 
{ 
       oldCheckedData = myLastIndexPath; 
       [self.tableView reloadData]; //<<---- this is where I reload the table to show the tick... 
} 

回答

2

嘗試cell.accessoryType =線移動到willDisplayCell:委託功能,像這樣:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    // You can move this one here too: 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection 

    if (indexPath == oldCheckedData) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

我讀了willDisplayCell:方法應該用於任何基本的視覺相關的修改,如selectionStyle/accessoryType,和cellForRowAtIndexPath:方法的細胞數據相關的操作,如設置文本,圖像等...

+0

好的謝謝你我會嘗試..因爲我注意到當我調試ifstatment它被調用當前視圖中的每個單元格..所以看起來效率很低......我會讓你知道我是如何去的。 –

+0

@ C.Johns酷,如果它仍然壞了,你可以在你設置'oldCheckedData'的地方發佈代碼。在加載表的過程中,對'oldCheckedData'的'if'檢查可能會從發生到'oldCheckedData'的事件中斷。 – chown

+0

沒問題,所以補充說,它的做法和之前的做法完全一樣......顯示了一些高點但不低的點......將按照它現在調用的順序發佈代碼。,,謝謝你幫助我,希望你看到我沒有的東西。 –