【问题标题】:How to make multiple select in a tableview cell by using checkmark in iPhone?如何在 iPhone 中使用复选标记在表格视图单元格中进行多项选择?
【发布时间】:2011-07-27 12:10:21
【问题描述】:

我有一个类,其中有一个表视图,在表视图中我通过数组显示值。当我运行我的应用程序并选择 tableview 的单元格时,一次只选择一个特定的单元格,并且它的值显示在我以前的控制器的 detailtextlabel 中。我希望一次启用多个选择。

现在我正在这样做。这是我希望我的多个表格视图单元格应该被选中的类:

TAddAlarmNewController.m

- (void)viewDidLoad {


    days =[[NSMutableArray alloc]initWithObjects:@"Every Monday",@"Every Tuesday",@"Every Wednesday",@"Every Thursday",@"Every Friday",@"Every Saturday",@"Every Sunday",nil];
    temp = [[NSDictionary alloc] initWithObjectsAndKeys:days,@"arrValue",nil];

    [super viewDidLoad];


}

在 days 数组中我存储 7 个值,在 rowinsection 的 no 中我正在计算数组:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
    // Return the number of rows in the section.
    return [days count];
}

在cellforrowatindexpath方法中:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [days objectAtIndex:indexPath.row];
    cell.accessoryType = ([indexPath isEqual:rowselection]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;


    return cell;
}

在此我将数组添加到单元格的文本标签中。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //rowselection is an indexpath variable.
     self.rowselection = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadData];
    for (int  i=0; i<[[self.navigationController viewControllers] count]; i++) 
    {
        UIViewController *aController = [[self.navigationController viewControllers]objectAtIndex:i];
        if ([aController isKindOfClass:[TAddAlarmController class]])
        {

            TAddAlarmController *addalarm = (TAddAlarmController*)aController;
            newrepeat =(NSString*)[[days objectAtIndex:indexPath.row] retain];
            [addalarm refreshTableToSetDetailText:newrepeat];
        }
    }

//TAddAlarmController is the previous controller in which detailtextlabel i am setting the next controller value.
}

【问题讨论】:

标签: iphone objective-c tableview


【解决方案1】:

Vijay 提供的链接解决了你的问题。您还需要保留一个数组(带有布尔值)来维护是否选择了一行。所以在重新加载表格视图时,你可以正确地打勾。

【讨论】:

  • 是的,她是对的,你必须跟踪这一点,因为单元格是自动释放的对象。所以在再次创建你的旧选定单元格时,它必须被选中。
  • 不,我不是在谈论数据源数组。声明新数组。你知道数据源数组的大小。 (这里可能是 7 个)。将 7 个 NSNumber 对象添加到数组中,所有对象都带有 NO 。 Whenever a row is selected, toggle the value at the index in the array.
  • Gomathi 我很困惑我是否创建了 2 个数组或单个数组来保存 bool 值,然后如何在 tableview 单元格上显示包含星期天、星期一、星期二的数组。请你提供一些虚拟代码,或者你能改变我的代码本身你想说的话。请
  • Gomathi ya 现在可以进行多项选择了
  • 但是他们存在一个问题,多个选定的行值没有显示在我的 tableview 控制器的 detailtextlabel 中。谢谢
【解决方案2】:

你应该试试这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{       
    selectedIndex = indexPath.row;

    CustomViewCell* theCell = (CustomViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    theCell.mySelectedImage.hidden = NO;

    //[tableView reloadData];
    [selectedIndices insertObject:[NSNumber numberWithInt:indexPath.row] atIndex:arrayIndex]; //the selectedIndices is array that stores index of selected rows
    NSLog(@"%@",[selectedIndices objectAtIndex:arrayIndex]);
    arrayIndex++;

    [tableView reloadData]; 
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    CustomViewCell  *cell = (CustomViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell =  [[[CustomViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    cell.mySelectedImage.hidden = YES;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    // this loop will check for selected index when table gets reload 
    for (int i = 0 ; i < [selectedIndices count]; i++)
    {
        NSLog(@"%@",[selectedIndices objectAtIndex:i]);

        int t = indexPath.row;
        int ind = [[selectedIndices objectAtIndex:i] intValue];
        if (t == ind) 
        {
            cell.mySelectedImage.hidden = NO;
        }
    }

    return cell;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多