5

I'm making a tableview with multiple row selected option. So, I used the checkmark accessory type action. I also require to edit/rename the text in the selected row.

Basically, I need to put checkmark (checkbox) on the left side and detail disclosure on the right side of the cell, both functional.

Below code is for checkmark that i have, currently checkmark appears on the right side of the cell.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

    TSIPAppDelegate *appDelegate = (TSIPAppDelegate *)[[UIApplication sharedApplication]delegate];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    if (cell.accessoryType==UITableViewCellAccessoryNone)
    {
      cell.accessoryType=UITableViewCellAccessoryCheckmark;
      appDelegate.selectedFile = cellText;
      if (prevSP!=indexPath.row) {
        cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prevSP inSection:0]];
        cell.accessoryType=UITableViewCellAccessoryNone;
        prevSP=indexPath.row;
      }
    }
    else if (prevSP!=indexPath.row){
      cell.accessoryType=UITableViewCellAccessoryNone;
    }
}

Any suggestions, please?

When a row selected, checkmark should be enabled/disabled AND disclosure button selected, it should open a new view for editing the selected row.

Jesse
  • 8,605
  • 7
  • 47
  • 57
Arun
  • 75
  • 1
  • 7
  • Can you please include the code you have? This will give others a better understanding of what you currently have. – Jesse Feb 23 '13 at 05:13
  • @Jesse: I have added the code. please check. For detail disclosure, yet to write. – Arun Feb 23 '13 at 05:22
  • @Arun The better way is instead of UITableViewCellAccessoryCheckmark you can add an UIImagview which have checkmark image at starting of each cell and accessory type as end of each cell – Vishnu Feb 23 '13 at 05:54

3 Answers3

3

accessoryType type is of enum UITableViewCellAccessoryType, by definition it will not accept multiple values as it not bitwise enum. So, you have to choose one and mimic the other by custom code.

Saran
  • 6,274
  • 3
  • 39
  • 48
3

I'd recommend using the UITableViewCellAccessoryCheckmark accessory type on the tableview and adding a "Detail Disclosure" button to the cell. Unfortunately you can't do exactly what you're looking for, but this is the cleanest alternative approach that I've found.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
1

This is sample code which i have used in one of my app

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

{

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[cellarray objectAtIndex:indexPath.row]];
    cell.textLabel.textColor=[UIColor blackColor] ;
    cell.textLabel.tag=indexPath.row;
    cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    // cell.textLabel.highlightedTextColor=[UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] ;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;


}

UIImageView *ima=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tick.png"]];
ima.frame=CGRectMake(280, 15, 14, 14);
ima.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;

int row = [indexPath row];
//cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.textLabel.textColor= (row == selectedRow) ? [UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] : [UIColor blackColor] ;
if (row==selectedRow) {

    [cell.contentView addSubview:ima];

}

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
[tempImageView setFrame:tableView.frame];

tableView.backgroundView = tempImageView;
[tempImageView release];
return cell;
}


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

selectedRow = [indexPath row]; // selected row is of type int  declared in .h

[tableView reloadData];


}

This code will have only one checkmark in entire tableView.. You can modify it to have multiple checkmark in that

Hope this helps !!!

Vishnu
  • 2,243
  • 2
  • 21
  • 44
  • It seems, you are adding an image in subview of the row. But, what i need is, storing the checkmarked rows in an array. And allowing the 'rename/edit' of the text of the selected row, somehow. Is the 'rename' of text possible? – Arun Feb 23 '13 at 07:02
  • @Arun Yes u told that u need checkmark in left side and accessory type as rightside.and this is one of the way. And you can store the textlabel text in array for renaming purpose in didSelectRowAtIndexPath – Vishnu Feb 23 '13 at 07:13
  • I wanted to have detail disclosure for 'renaming' purpose only. Having the 'renaming/editing' option enabled is higher priority than keep it left or right aligned. Thanks. – Arun Feb 23 '13 at 07:41
  • @Arun If i understood correct. On touching detail disclosure you need to rename the cell text label. and the detail disclosure have to be checkmark type right? – Vishnu Feb 23 '13 at 08:35
  • touching anywhere in the row, should make the checkmark enabled or disabled. touching the disclosure mark should allow the text to be edited. – Arun Feb 23 '13 at 08:56
  • @Arun Will the rename function will take place in same page or you r showing new view for renaming ? – Vishnu Feb 23 '13 at 09:48
  • either a pop-up or a new screen, both are fine. – Arun Feb 23 '13 at 11:30
  • @Arun There is quite a lot of work you have to do. On selecting an accessory type you can use this method - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath – Vishnu Feb 23 '13 at 11:48
  • Thank you. Anyway, I got an idea from the below link, to implement the 'renaming'. Thanks for the support. [link] http://stackoverflow.com/questions/7835494/add-button-in-table-view – Arun Feb 24 '13 at 07:01
  • Jesus Christ... I cant believe Apple implemented some similar fix like this in the the Settings > WiFi list, and still didn't provide a proper solution!! Arghhh! – Josh Mar 15 '16 at 16:15