tableview with checkmark and detail disclosure together
Asked Answered
A

3

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.

Averse answered 23/2, 2013 at 4:54 Comment(3)
Can you please include the code you have? This will give others a better understanding of what you currently have.Vicechancellor
@Jesse: I have added the code. please check. For detail disclosure, yet to write.Averse
@Averse 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 cellPsilocybin
P
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 !!!

Psilocybin answered 23/2, 2013 at 6:1 Comment(10)
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?Averse
@Averse 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 didSelectRowAtIndexPathPsilocybin
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.Averse
@Averse 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?Psilocybin
touching anywhere in the row, should make the checkmark enabled or disabled. touching the disclosure mark should allow the text to be edited.Averse
@Averse Will the rename function will take place in same page or you r showing new view for renaming ?Psilocybin
either a pop-up or a new screen, both are fine.Averse
@Averse 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 *)indexPathPsilocybin
Thank you. Anyway, I got an idea from the below link, to implement the 'renaming'. Thanks for the support. [link] https://mcmap.net/q/2035379/-add-button-in-table-viewAverse
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!Advocate
O
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.

Ossiferous answered 23/2, 2013 at 5:45 Comment(0)
P
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.

Pious answered 8/9, 2014 at 1:53 Comment(0)
P
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 !!!

Psilocybin answered 23/2, 2013 at 6:1 Comment(10)
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?Averse
@Averse 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 didSelectRowAtIndexPathPsilocybin
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.Averse
@Averse 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?Psilocybin
touching anywhere in the row, should make the checkmark enabled or disabled. touching the disclosure mark should allow the text to be edited.Averse
@Averse Will the rename function will take place in same page or you r showing new view for renaming ?Psilocybin
either a pop-up or a new screen, both are fine.Averse
@Averse 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 *)indexPathPsilocybin
Thank you. Anyway, I got an idea from the below link, to implement the 'renaming'. Thanks for the support. [link] https://mcmap.net/q/2035379/-add-button-in-table-viewAverse
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!Advocate

© 2022 - 2024 — McMap. All rights reserved.