2

我正在做一个项目,但我坚持了一点。在单击单元格表视图后,我的项目有一个包含一些数据的表视图,它将扩展到部分。除了一件事我想设置一个复选框图像(当用户单击一个单元格时)之外,每一件事都很好。当我点击单元格时,它会给出单元格标题文本,但我无法在特定单元格部分设置复选框图像(如UITableViewCellAccessoryCheckmark)。

我在下面发布图像和代码:!

MyTableView 它包含根练习名称

MyTableView 它包含根练习名称

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

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

当我点击 **tableView didSelectRowAtIndexPath:** 它工作正常

当我点击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;
    }
}

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

对不起!如果格式不正确(新堆栈)!谢谢你!

4

2 回答 2

1

您对如何执行此操作有正确的想法,但是,您将复选标记设置在错误的位置。您应该使用该方法-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath来存储(记住)选择了哪个单元格,而不是设置附件类型。

如果一次只能选择一个单元格,您可以声明一个NSIndexPath属性来记住被选中的单元格。但是,如果一次可以选择多个单元格,您可以声明一个NSMutableArray可以存储所有已选择单元格的位置。

然后您应该使用该方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath将复选标记设置为选中的单元格。

这是一个示例(我假设可以选择多个单元格):

在您的 *.h 文件中声明一个NSMutableArray属性

@property (nonatomic, strong) NSMutableArray *selectedCells;

在您的 *.m 文件中,在您的 init 方法中初始化属性:

_selectedCells = [[NSMutableArray alloc] init];

然后,在didSelectRowAtIndexPath...方法中:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ([self.selectedCells containsObject:indexPath]) {
        [self.selectedCells removeObject:indexPath]; // this line allows the checkmark to be shown/hidden if the user presses several times the same cell
    } else {
        [self.selectedCells addObject:indexPath]; // a cell was selected; remember it
    }

    ... // the rest of your code
}

最后,在cellForRowAtIndexPath...方法中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ... // Create, initialize and customize your cell

    // if the cell you're being asked for is saved in the array it means it was selected
    if ([self.selectedCells containsObject:indexPath]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
}

就是这样。如果您有更多问题,请告诉我

希望能帮助到你!

更新

如果您只想将自定义图像设置为复选标记,只需创建图像并将其分配给附件视图,同时使用已经为您工作的代码,如下所示:

// Note that you should use 'accessoryView' and NOT 'accessoryType'
if (thisCell.accessoryView == UITableViewCellAccessoryNone)
{
    thisCell.accessoryView = [[UIImageView alloc] initWithImage:
                                        [UIImage imageNamed:@"YOUR_IMAGE_NAME"]];

}
else
{
    thisCell.accessoryView = nil;
}
于 2013-09-26T18:34:55.327 回答
1

我终于能够让它工作了。以下是您必须更改的内容:

-(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];

}

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

更新

这是对发生的事情的解释。实际上有几个问题:

首先,你在混合accessoryType; accessoryView这导致该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];

差不多就是这样。

希望这可以帮助!

于 2013-09-27T08:07:49.350 回答