如何在选择表格视图部分时设置复选框图像?

时间:2013-09-26 14:30:52

标签: iphone ios objective-c uitableview

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

我发布的图片和代码如下:!

MyTableView it contains the Root Exercise Names

MyTableView它包含根练习名称

This is the Expended View and i want a checkbox(UITableViewCellAccessoryCheckmark) at the right side of the cell.

这是Expended View,我想在单元格的右侧有一个复选框(UITableViewCellAccessoryCheckmark)。

When i click on **tableView didSelectRowAtIndexPath:** it works fine

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

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

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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];

}

如果这些方法没有改变,只需复制粘贴代码即可。我在发布之前对其进行了测试。如果您有更多问题,请与我们联系

<强>更新

以下是对正在发生的事情的解释。实际上有几个问题:

首先,你混合了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;
}

其次,您没有保存所选元素的indexPath。这是更正的代码(注意是相同的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];

这就是它。

希望这有帮助!