获取具有复选标记附件的单元格

时间:2013-12-06 11:08:00

标签: ios iphone uitableview

我正在使用表格视图。我在第一次点击屏幕时添加了复选标记附件,并在第二次点按时删除了此复选标记。我在表视图的单元格上添加了几个ckeckmarks。现在我希望具有复选标记的单元格的标签应该显示在NSlog上。  请帮我解决这个问题。任何帮助都会非常有用。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0)
    {
        return [celltitles count];
    }
    else
    {
    return 7;
    }
}

- (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];
    }
    if (tableView.tag == 0)
    {

        cell.textLabel.text = [celltitles objectAtIndex:indexPath.row];

        if (indexPath.row == 0)
        {
            habitname = [[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];
            habitname.placeholder = @"Habit Name";
            habitname.delegate= self;
            [cell addSubview:habitname];
        }
                else if (indexPath.row == 1)
        {
            timelbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 50)];
            timelbl.text = @"OFF";
            timelbl.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:timelbl];

            timetitlelbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
            timetitlelbl.text = @"Alert";
            timetitlelbl.textAlignment = NSTextAlignmentCenter;
            [cell addSubview:timetitlelbl];

            toggleswitch = [[UISwitch alloc] initWithFrame:CGRectMake(250, 5, 50, 60)];
            toggleswitch.on = NO;
            [toggleswitch addTarget:self action:@selector(toggleSwitch) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
            [cell addSubview:toggleswitch];

        }
    }
    else if (tableView.tag == 1)
    {
        cell.textLabel.text = [daysarray objectAtIndex:indexPath.row];

    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



        if (indexPath.row == 1)
        {
            if (toggleswitch.on)
            {
                [self datepickershown:timelbl.text animated:YES];

            if (self.datePickerIsShowing)
            {
                //[self datepickershown];
                //[self hideDatePickerCell];
                [dscptxt resignFirstResponder];
            }
            else
            {
                [dscptxt resignFirstResponder];
                [self.timelbl resignFirstResponder];
                [self showDatePickerCell];
            }
        }
        else
        {
        timelbl.textColor = [UIColor grayColor];
        }
    }
else if (indexPath.row > 2 && indexPath.row <10)
{
   NSLog(@"I am Selectin the row");


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.accessoryType == UITableViewCellAccessoryNone)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

        }
        else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
           // NSLog(@"Selected Accessory Is %d",cell.accessoryType);
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)savedata:(id)sender
{
}

以上是我正在使用的代码以及我想要日志的保存操作。

4 个答案:

答案 0 :(得分:1)

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [markedArray addObject:indexPath.row];
    }
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
       // NSLog(@"Selected Accessory Is %d",cell.accessoryType);
        cell.accessoryType = UITableViewCellAccessoryNone;
        [markedArray removeObjectAtIndex:indexPath.row];
    }
} 
- (void)savedata:(id)sender
  {
    //write here your code to save to database
    for(int i=0; i<markedArray.count; i++)
        NSLog@"saved labels are %@",[celltitles objectAtIndex:[markedArray      objectAtIndex:i]];
   }

试试这个:)

答案 1 :(得分:0)

您可以从以下方法获取所有选定的indexPath。

NSArray *selectedIndexPaths = [self.Yourtableview indexPathsForSelectedRows];
获取此选定的索引数组后

。您可以使用indexPath.row属性获取单元格的标题。

答案 2 :(得分:0)

您还可以在附加数组中存储选定对象(在表中显示数据的对象)。 只需在didSelectRow方法中添加/删除数据对象到数组,您将在以后随时获得表状态数据。

答案 3 :(得分:0)

我不建议您使用复选标记方法找出所选单元格

试试这个:

  1. 创建名为“selectedCells”(或其他内容)的NSMutableArray。它可以是属性或实例变量。

  2. -viewDidLoad方法实例化。

  3. 在底部,-tableView:cellForRowAtIndexPath方法添加此代码。

    if([selectedCells containsObject:@(indexPath.row)]){     cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }其他{       cell.accessoryType = UITableViewCellAccessoryNone; }

  4. -tableView:didSelectRowForIndexPath:方法调用中:

    if([selectedCells containsObject:@(indexPath.row)]){     [selectedCells removeObject:@(indexPath.row)];
    }其他{     [selectedCells addObject:@(indexPath.row)]; }

  5. 使用这种方式,您可以在“selectedCells”中获得所选单元格(或带有复选标记的单元格)的信息

相关问题