0

I am working with the table view. I added the checkmark Accessory on the first tap of the screen and removed this checkmark on the second tap. I added few ckeckmarks on the cells of table view. Now i want that the labels of the cells having the checkmarks should be displayed on the NSlog. please help me out regarding this issue. Any help will be much appriciable.

- (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
{
}

Above is the code which i am using and on the save action i want the log.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
sahil dhiman
  • 91
  • 1
  • 1
  • 7
  • Maintain an array to hold the check marked indexpath.row, add the index path.row to the array when ever you checks a new row, and remove that index path.row when you are unchecking the row from the array. – Madhu Dec 06 '13 at 12:43
  • You should probably [check this answer](http://stackoverflow.com/questions/5959950/iphone-uitableview-cellaccessory-checkmark). – brainforked Dec 06 '13 at 12:56

2 Answers2

3

Don't use the UITableView to store your data. Cells are reused, so that's not a reliable way to detect which items you have selected.

You could store whether an item is selected in an array, for instance. Then in your cellForRowAtIndexPath determine if the checkmark should be displayed. In didSelectRowAtIndexPath you update the model (your array) and request a reload for the specific cell.

This will result in a better MVC separation.

EDIT: added example for the UITableView stuff

// for this example we'll be storing NSIndexPath objects in our Model
// you might want to use a @property for this
NSMutableSet *model = [NSMutableSet set];


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

    ...

    if([model containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    ... 
}


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

    ...

    // update the model
    if([model containsObject:indexPath]) {
        [model removeObject:indexPath];
    } 
    else {
        [model addObject:indexPath];
    }

    // request reload of selected cell
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade];

    ...

}

Now it should be fairly easy to log all selected items, without having to use the UITableView: it's all in your model now!

fguchelaar
  • 4,779
  • 23
  • 36
  • A view (mVc) is for displaying data, not storing it. By only setting the Checkmark on a cell, you're basically storing data in your view. You shouldn't: store data in the model (Mvc). I'll update my answer with some code. – fguchelaar Dec 06 '13 at 12:53
  • ok i am getting the output and seems to be preety well bt i am not getting the desired output...... output which i am getting with this code is somwwhat like this.:- " {length = 2, path = 0 - 3}", " {length = 2, path = 0 - 4}", " {length = 2, path = 0 - 5}", " {length = 2, path = 0 - 6}", whereas i want the the label in my output..... – sahil dhiman Dec 07 '13 at 09:26
  • That's because you're probably logging the content of the model array and that's an NSIndexPath. Get the title from your cell titles array. Somehing like: `for(NSIndexPath *indexPath in model) { NSLog(celltitles[indexPath.Row]); }` – fguchelaar Dec 07 '13 at 12:20
0

Make a for loop:

NSMutableArray *mutArray = [[NSMutableArray alloc] init];

for(i = 0;i <= self.rowCount-1;i++)
 {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
           [mutArray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
 }

}
NSLog(@"Array: %@", mutArray);
iCode
  • 1,456
  • 1
  • 15
  • 26