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

标签 ios iphone uitableview

我正在使用表格 View 。我在第一次点击屏幕时添加了复选标记附件,并在第二次点击时删除了该复选标记。我在表格 View 的单元格上添加了几个 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
{
}

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

最佳答案

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

试试这个:)

关于ios - 获取具有复选标记附件的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20422355/

相关文章:

ios - Xcode 8 中奇怪的终端消息

iphone - 如何找到 UITextView 的行数

objective-c - 如何在细节 Controller 中引用属性列表?

iphone - iPhone 的翻译?

ios - 如何使用 Parse 动态化单元格高度?

ios - 使用 Objective-C 从不同的 ViewController 重新加载 TableView

ios - 为什么我的ViewDidLoad每次都会加载?

iphone - 保存 AVPlayer 的缓冲区数据

iphone - 对于在 tableview 上快速加载图像,哪一个是可用于 iphone 的最佳方法?

ios - 带有 XIB Swift 的 UITableViewCell 子类