我正在做一个项目,但我陷入了困境。我的项目有一个表格视图,其中包含一些数据,单击单元格表格视图后将扩展为多个部分。除了一件东西外,其他所有东西都很好,即我想设置一个复选框图像(当用户单击单元格时)。
当我点击单元格时,它会给出单元格标题文本,但是我无法在特定单元格部分设置复选框图像(如 UITableViewCellAccessoryCheckmark )。

我要发布以下图像和代码:

MyTableView,它包含根练习名称

这是扩展视图,我想要在单元格右侧的复选框(UITableViewCellAccessoryCheckmark)。

当我单击 tableView didSelectRowAtIndexPath:时,它工作正常

//码

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([indexPath isEqual:self.selectIndex]) {
            self.isOpen = NO;
            [self didSelectCellRowFirstDo:NO nextDo:NO];
            self.selectIndex = nil;

        }else
        {
            if (!self.selectIndex) {
                self.selectIndex = indexPath;
                [self didSelectCellRowFirstDo:YES nextDo:NO];

            }else
            {

                [self didSelectCellRowFirstDo:NO nextDo:YES];
            }
        }

    }else
    {
        NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
        NSArray *list = [dic objectForKey:@"ExcerciseList"];            //from plist
        NSString *item = [list objectAtIndex:indexPath.row-1];
        UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
        if (thisCell.accessoryType == UITableViewCellAccessoryNone)
        {
            thisCell.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else
        {
            thisCell.accessoryType = UITableViewCellAccessoryNone;
        }

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil] ;
        [alert show];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


   #pragma mark- Table View DataSource and Delegates
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
        static NSString *CellIdentifier = @"Cell1";
        _cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!_cell) {
            _cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];

            //[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:@"ExcerciseList"];
        _cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];

        //[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
        return _cell;
    }
    else
    {

        static NSString *CellIdentifier = @"view_TableView_List";
        cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:@"Excercise_Name"];
        cell.Lbl_Excercise_Name.text = name;
        cell.Lbl_Excercise_Name.font=[UIFont fontWithName:@"Hustlers Rough Demo" size:40.0f];
        NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:@"Excercise_Image"];
        [cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
        [cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
        return cell;
    }
}

我的问题是如何在所选部分显示复选框?

抱歉!如果格式不正确(新堆栈)!
谢谢!

最佳答案

我终于能够使它工作。这是您必须更改的内容:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
        static NSString *CellIdentifier = @"Cell1";
        _cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!_cell) {
            _cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];

            //[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
        }
        NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:@"ExcerciseList"];
        _cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
        _cell.accessoryView = nil;
        if ([self.selectedCells containsObject:indexPath])
        {
            _cell.accessoryView = [[UIImageView alloc] initWithImage:
                               [UIImage imageNamed:@"check_box@2x.png"]];
        }
        //[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
        return _cell;
    }
    else
    {

        static NSString *CellIdentifier = @"view_TableView_List";
        cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:@"Excercise_Name"];
        cell.Lbl_Excercise_Name.text = name;
        cell.Lbl_Excercise_Name.font=[UIFont fontWithName:@"Hustlers Rough Demo" size:40.0f];
        NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:@"Excercise_Image"];
        [cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
        [cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
        return cell;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([indexPath isEqual:self.selectIndex]) {
            self.isOpen = NO;
            [self didSelectCellRowFirstDo:NO nextDo:NO];
            self.selectIndex = nil;

        }else
        {
            if (!self.selectIndex) {
                self.selectIndex = indexPath;
                [self didSelectCellRowFirstDo:YES nextDo:NO];

            }else
            {

                [self didSelectCellRowFirstDo:NO nextDo:YES];
            }
        }

    }else
    {

        NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
        NSArray *list = [dic objectForKey:@"ExcerciseList"];
        NSString *item = [list objectAtIndex:indexPath.row-1];
        UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
        if (!thisCell.accessoryView)
        {
            [self.selectedCells addObject:indexPath];
            thisCell.accessoryView = [[UIImageView alloc] initWithImage:
                                  [UIImage imageNamed:@"check_box@2x.png"]];

        }
        else
        {
            [self.selectedCells removeObject:indexPath];
            thisCell.accessoryView = nil;
        }

//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil] ;
//        [alert show];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

如果这些方法没有其他改变,只需复制粘贴代码即可。我在发布之前对其进行了测试。如果您还有其他问题,请告诉我

更新

这是发生了什么的解释。实际上有两个问题:

首先,您要混合accessoryTypeaccessoryView;这导致了
if-else子句的执行不正确。这是旧的代码
// here you are using accessoryType
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
    // but here you're using accessoryView
    thisCell.accessoryView = [[UIImageView alloc] initWithImage:
                              [UIImage imageNamed:@"check_box@2x.png"]];

}
else
{
    // here too...
    thisCell.accessoryView=nil;
    thisCell.accessoryView = nil;
}

其次,您没有保存所选元素的indexPaths。这是更正后的代码(注意与if-else子句相同):
if (!thisCell.accessoryView)
{
    // you have to save the selected indexPaths
    [self.selectedCells addObject:indexPath];
    thisCell.accessoryView = [[UIImageView alloc] initWithImage:
                              [UIImage imageNamed:@"check_box@2x.png"]];

}
else
{
    // if the cell was already selected, then remove the indexPath
    [self.selectedCells removeObject:indexPath];
    thisCell.accessoryView = nil;
}

第三,cellForRowAtIndexPath...内的以下行将事情弄乱了,因为它显示了另一个选中标记,因此我将其删除:
[_cell.checkUnCheck_ImageView setHidden:NO];

仅此而已。

希望这可以帮助!

关于iphone - 选择表格 View 部分时如何设置复选框图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19031062/

10-11 23:03
查看更多