uitableviewcell上的附件标记在某些情况下没有显示

时间:2011-10-14 00:54:01

标签: iphone ios uitableview nsindexpath

我有两个tableview,一个有几个tableview单元格,每个单元格打开相同的子视图但是初始化了新数据..

表格中大约有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
}

最后我将它传递给我的子视图中的方法,该方法将indexpath传递给oldCheckedData

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

2 个答案:

答案 0 :(得分: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:方法设置文本,图像等操作......

答案 1 :(得分:0)

我最近遇到了这个问题,如果发现在我的情况下该单元格具有一个附件视图集。此代码段可确保删除视图。

 public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

..logic here to to determine if cell should be selected...

if ( cell.accessoryView != nil) {
    cell.accessoryView?.removeFromSuperview()
    cell.accessoryView = nil
}
cell.accessoryType = .checkmark