SpokaneDude
SpokaneDude

Reputation: 4974

I want to CheckMark selected rows in a UITableView based on contents of a UITextView

I have a UITextView populated with data from a UITableView (tvServices); when the user taps on a row in the UITableView, I move the contents of that cell to the UITextView, after which the contents of the UITextView are stored in a CoreData store.

When the user selects a record for updating, I move the contents of the stored UITextView back into the UITextView. When the user taps on the UITextView, I display the entire UITableView as a UIPopover, with the contents of the UITextView marked with a checkmark (AccessoryCheck) in the UITableView.

Unfortunately, this is not working the way I designed it... nothing gets checked. Here is my code ( this is a proof of concept - where I check every row in the UITableView to make sure it can be done). globalServicesArray is the data source for the UITableView cells:

for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];

    UITableViewCell *cell = [tvServices cellForRowAtIndexPath:path];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

I hope this makes sense; if there is a better way of doing this, I'm open for suggestions... otherwise what am I doing wrong?

Upvotes: 0

Views: 57

Answers (1)

nprd
nprd

Reputation: 1942

Assuming only one cell can be edited, can you try this. Store the index path in a class level iVar/Property, say. selectedIndex and reload the cell

//To hold the index paths
NSMutableArray *reloadArray = [NSMutableArray array];
if (self.selectedIndex)
{
    //if already selected deselect
    [reloadArray addObject:self.selectedIndex];
}
for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) {
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
    [reloadArray addObject:path];
}
[tvServices reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationNone];

In cell for row at index path check for the index path and set accessory checked if matches the index

if (self.selectedIndex &&
    self.selectedIndex.row == indexPath.row &&
    self.selectedIndex.section == indexPath.section)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

Upvotes: 0

Related Questions