How to Set TableViewCell Focus colour in tvOS - objective-c

Default focus color of UITableViewCell is white colour.
How can we change the focus colour of UITableView cell?

You can set this as in the code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
cell.focusStyle = UITableViewCellFocusStyleCustom;
// ...
return cell;
}
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
if ([context.previouslyFocusedView isKindOfClass:[UITableViewCell class]])
context.previouslyFocusedView.backgroundColor = [UIColor clearColor];
if ([context.nextFocusedView isKindOfClass:[UITableViewCell class]])
context.nextFocusedView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
}

It is a swift code. Please convert to Objective-C and try it. It maybe help you.
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if let nextFoc = context.nextFocusedView as? YourCellName{
nextFoc.backgroundColor = UIColor.redColor()
}
if let prevFocus = context.previouslyFocusedView as? YourCellName{
prevFocus.backgroundColor = UIColor.clearColor()
}
Look it a screen shot.

If you want to maintain the fancy parallax cells you can first set a background view on your custom cell:
self.backgroundView = UIView()
And in your viewcontroller with the tableview do something like this:
func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
(context.nextFocusedView as! MyCustomTableviewCell).backgroundView!.backgroundColor = UIColor.purpleColor()
if let prev = context.previouslyFocusedView as? MyCustomTableviewCell {
// Set the color back to whatever it was, in this case I have a black background with black cells
prev.backgroundView?.backgroundColor = UIColor.blackColor()
}
}

If you are using custom tableViewCell, then you can use the delegate method of the tableViewCell as below,
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
if (self.focused)
{
[self showHighlightedCellStyle];//set highlighted bg color
}
else
{
[self showNormalCellStyle];//reset the bg color
}
}

Related

tableview, change font color of focus tvos

How do you change the font color of a "focused" UITableView cell? I currently have this....
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
if (context.nextFocusedView) {
CategoryCell *cell = [self.categoryTableView dequeueReusableCellWithIdentifier:#"CategoryCell"];
[cell.categoryLbl setTextColor:[UIColor orangeColor]];
}
}
I know that if you want to change a background of a focused UITableViewCell it would be:
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
[context.nextFocusedView setBackgroundColor:[UIColor clearColor]];
[context.previouslyFocusedView setBackgroundColor:[UIColor orangeColor]];
}
In TVOS UITableViewDelegate have new method tableView:didUpdateFocusInContext:withAnimationCoordinator: which will be called when focus change, you can get previous IndexPath and nextFocusIndexPath and then you can use tableView methods to get cells, your code should look like this
- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
NSIndexPath *prevIndexPath = [context previouslyFocusedIndexPath];
if (prevIndexPath)
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:prevIndexPath];
cell.textLabel.textColor = [UIColor white];
}
NSIndexPath *nextIndexPath = [context nextFocusedIndexPath];
if (nextIndexPath)
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
cell.textLabel.textColor = [UIColor blackColor];
}
}
Please visit this Apple docs for more detail
Here is my approach using Swift 2.0 :)
func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if let prevIndexPath = context.previouslyFocusedIndexPath {
let prevCell = tableView.cellForRowAtIndexPath(prevIndexPath)
prevCell?.backgroundColor = UIColor.blackColor()
}
if let nextIndexPath = context.nextFocusedIndexPath {
let nextCell = tableView.cellForRowAtIndexPath(nextIndexPath)
nextCell?.backgroundColor = UIColor.whiteColor()
}
}
Here's a good solution in swift 2.0.
func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if ((context.previouslyFocusedIndexPath) != nil) {
let cell = tableView.cellForRowAtIndexPath(context.previouslyFocusedIndexPath!)
cell?.textLabel?.textColor = UIColor.whiteColor()
}
if ((context.nextFocusedIndexPath) != nil) {
let cell = tableView.cellForRowAtIndexPath(context.nextFocusedIndexPath!)
cell?.textLabel?.textColor = UIColor.blackColor()
}
}
Here is what you want to achieve in Swift 5.2:
func tableView(_ tableView: UITableView, didUpdateFocusIn context: UITableViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if let prevIndexPath = context.previouslyFocusedIndexPath {
let prevCell = tableView.cellForRow(at: prevIndexPath) as! TVHomeCell
prevCell.titleLabel.font = .systemFont(ofSize: 32, weight: .medium)
}
if let nextIndexPath = context.nextFocusedIndexPath {
let nextCell = tableView.cellForRow(at: nextIndexPath) as! TVHomeCell
nextCell.titleLabel.font = .systemFont(ofSize: 34, weight: .bold)
}
}
You can change colour by using context , it contain reference of previously focused cell and the current cell that is focused. just cast it to your cell type and then perform the colour change operation.
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if let prevIndex = context.previouslyFocusedItem{
if let myCell = prevIndex as? Mycell {
myCell.textLabel.textColor = UIColor.white
}
}
if let nextIndex = context.nextFocusedItem {
if let myCell = nextIndex as? MyCell {
myCell.textLabel.textColor = UIColor.black
}
}
}

Cannot link my table view controller on storyboard to my code

I am writing an app in XCode6. Currently I have "SelectionTableViewController.h" and "SelectionTableViewController.m" such that you could add/remove checkmarks on select. Also, I have a table view controller segue in the storyboard, that is triggered by a static cell in the previous table view controller. I set up the trigger in storyboard so I did not write the code for "prepare for segue" or anything.
I want the cell to be checked by default, so I have done the following:
Changed the view controller to "SelectionTableViewController"
Set the identifier of prototype cell on my storyboard to "SelectionCell"
Changed the cell's background color to orange and accessory to checkmark
Below is my SelectionTableViewController.m:
#implementation SelectionTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"SelectionCell"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
self.navigationItem.title = #"Select";
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [[[MyStore sharedStore] allCategories] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *category = [[[MyStore sharedStore] allCategories] objectAtIndex:section];
return [[[MyStore sharedStore] allNamesForCategory: category] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath*)indexPath
{
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:#"SelectionCell"forIndexPath:indexPath];
// Configure the cell...
NSString *category = [[[MyStore sharedStore] allCategories] objectAtIndex:indexPath.section];
NSArray *items = [[MyStore sharedStore] allNamesForCategory: category];
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[MyStore sharedStore] allCategories] objectAtIndex:section];
}
#end
My program runs on the simulator, and the items listed in the cells are correct. However the cells does not have the default checkmark on the cell before I do a selection. Nor are the cells orange. I know I can set up the default checkmark and the background color in code easily, but I just want to figure out how to do this in the interface builder, as I would be dealing with the UI a lot when the program is set up.
Hope someone can help me on that as I'm kinda new to iOS programming and this is the first time I have ever used Storyboard. Thanks!
The problem with your implementation is tableView:didSelectRowAtIndexPath will only be triggered by user's interaction only, therefore the initial selection doesn't show. You will need to make tableView aware of the selection state during initialisation too, else de-selection will not function as expected.
class ViewController: UITableViewController {
var selectedItem: Int = 5
func updateSelection(selected: Bool, forCell cell: UITableViewCell) {
cell.accessoryType = selected ? .Checkmark : .None
// update other cell appearances here..
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Note: happens after the initial tableView.reloadData()
// Let tableView know who's selected before we appear, so that tableView is aware of the selection state.
// Doing so will enable tableView to know which cell to deselect after a new selection.
tableView.selectRowAtIndexPath(NSIndexPath(forItem: selectedItem, inSection: 0), animated: true, scrollPosition: .Middle)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel?.text = "Item \(indexPath.item)"
// Setup the selection appearance during cell creation
updateSelection(indexPath.item == selectedItem, forCell: cell)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Keep track of the selection, and update cell appearance
selectedItem = indexPath.item
updateSelection(true, forCell: tableView.cellForRowAtIndexPath(indexPath)!)
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
// As above
updateSelection(false, forCell: tableView.cellForRowAtIndexPath(indexPath)!)
}
}
I've figured it out myself! I deleted the code that registered "Selection Cell". Then I did some minor changes to the cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath*)indexPath
{
Static NSString *selectionCell = #"SelectionCell";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:selectionCell forIndexPath:indexPath];
// Omit...
}

NSTextField dynimic height in NSTableView

There is a NSTextField in a NSTableView.
The height of the text in the window resizing is increased.
but Not grow the cells in a table view.
In this case, the height of table view cell to alter how do you like?
in short, When resizing the size of the text view, I want to change the size of the cell.
like this: (Mac store)
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;
{
KFCustomCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];
NSString *comment = [[_commentList objectAtIndex:row]valueForKey:#"COMMENT"];
[cellView.comment setStringValue:comment];
float cellheight = [comment heightForStringDrawing:comment font:[cellView.comment font] width:self.view.frame.size.width * 0.8];
if (cellheight > cellView.comment.frame.size.height)
{
return (cellheight + 65);
}
return 90;
}
Use following approach the to resize cell height according to textview content :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DataCellId = #"DataCell";
dataCell = (Cell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
if(!dataCell)
{
dataCell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId] autorelease];
}
dataCell.dataCellTextView.tag = indexPath.row;
dataCell.dataCellTextView.text =[yourDataArry objectAtIndex:indexPath.row];
return dataCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define PaddingForTableRowHeight 24
NSString *text;
if([yourDataArry count] > indexPath.row)
{
text = [yourDataArry objectAtIndex:indexPath.row];
} else {
text = #"";
}
UIFont *dataFont = [UIFont boldSystemFontOfSize:16.0f];
CGSize textSize ;
if([UICommonUtils checkIfiOS7])
{
// For iOS7
textSize = [text boundingRectWithSize:CGSizeMake("YOUR CELL WIDTH", MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:dataFont} context:nil].size;
} else {
textSize = [text sizeWithFont:dataFont constrainedToSize:CGSizeMake("YOUR CELL WIDTH" - PaddingForTableRowHeight,MAXFLOAT)];
}
float textViewHeight = MAX(kDefaultCellHeight, textSize.height+PaddingForTableRowHeight);
return textViewHeight;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self scrollToCursorForTextView:textView];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
dataUpdated =YES;
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
[self performSelector:#selector(dataReload) withObject:nil afterDelay:0.0];
[self scrollToCursorForTextView:textView];
}
-(void)dataReload
{
[self.tbl beginUpdates];
[self.tbl endUpdates];
}
There are two methods which are used to draw a cell if you know
1-viewForTableColumn
2-objectValueForTableColumn
In the above methods,when you draw your textfield you can give the dynamic height or your row as you are giving in heightforrowatindexpath method.By doing this your cell will get the accurate height. If you have any problem in calculating dynamic height then tell me I'll post the code here for calculating it.

Set background color of UITableViewCell

I have looked around to find a solution for setting the background color of the accessoryView to the same background color as the cellĀ“s contentView.
cell.contentView.backgroundColor = [UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
cell.accessoryView.backgroundColor =[UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
There is a solution that works but only let me use one color for all cells.
cell.contentView.superView.backgroundColor = [UIColor redColor];
Is the only solution to not use the accessoryView and use an image instead?
Thanks!
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor greenColor];
}
Using this UITableViewDelegate method, you can set the color of cells to different colors. Note that Apple explicitly advise you to make changes to the backgroundColor property within the tableView:willDisplayCell:ForRowAtIndexPath: method in the docs, which state:
If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate
Indeed, in iOS 6, changes to the property from anywhere else (like the tableView:cellForRowAtIndexPath: method) would have no effect at all. That no longer seems to be the case in iOS 7, but Apple's advice to modify the property from within tableView:willDisplayCell:ForRowAtIndexPath: remains (without any explanation).
For alternating colors, do something like this example:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2) {
cell.backgroundColor = [UIColor yellowColor];
} else {
cell.backgroundColor = [UIColor redColor];
}
}
I struggled with this one for a little while too and resorted to creating a custom image with the accessory. But I just found this solution that works well and doesn't require a custom image. The trick is to change the cell's backgroundView color not the backgroundColor.
UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2) {
myView.backgroundColor = [UIColor whiteColor];
} else {
myView.backgroundColor = [UIColor blackColor];
}
cell.backgroundView = myView;
No need to change the accessoryView or contentView background colors. They'll follow automatically.
Note for 2014. Very typically you wold use -(void)setSelected:(BOOL)selected animated:(BOOL)animated
So, you'd have a custom cell class, and you'd set the colours for the normal/selected like this...
HappyCell.h
#interface HappyCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *mainLabel;
etc...
#end
HappyCell.m
#implementation HappyCell
-(id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
}
return self;
}
-(void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
self.backgroundColor = [UIColor redColor];
.. other setup for selected cell
}
else
{
self.backgroundColor = [UIColor yellowColor];
.. other setup for normal unselected cell
}
}
#end
// to help beginners.......
// in your table view class, you'd be doing this...
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return yourDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger thisRow = indexPath.row;
ContentsCell *cell = [tv
dequeueReusableCellWithIdentifier:#"cellName"
forIndexPath:indexPath];
// "cellName" must be typed in on the cell, the storyboard
// it's the "identifier", NOT NOT NOT the restorationID
[cell setupForNumber: thisRow];
cell.mainLabel.text = yourDataArray[ thisRow ][#"whatever"];
cell.otherLabel.text = yourDataArray[ thisRow ][#"whatever"];
return cell;
}
hope it helps someone.
This worked for me:
cell.contentView.backgroundColor = [UIColor darkGreyColor];
For all lines with the same color
cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f];
For 2 colors
cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f];
if ((cell.backgroundColor = (indexPath.row % 2 == 0 ? [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f] : [UIColor colorWithRed:2.0/255.0 green:68.0/255.0 blue:80.0/255.0 alpha:1.0f]))){
cell.textLabel.textColor = [UIColor whiteColor];
}
For anyone else who might stumble on this and wants to set their UITableViewCell background to a pattern or texture rather than a solid color, you can do so like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"pattern.png"]];
}
The best option to have different backgrounds and what not would probably be to make your own TableViewCell implementation, in there you can put the logic to show whatever you want based on content or index etc.

UITableViewCell checkmark change on select

Am I correct in thinking that to change the checkmark for "on" to "off", I must change the CellAccessoryType between none and checkmark on the didSelectRowAtIndexPath?
Because I have done this but I have noticed the behaviour is not perfectly identical to like the checkmark cells on the auto lock settings on the iphone.
Or is there some other way checkmarks are meant to be handled?
Keep a property in your view controller called selectedRow, which represents the index of a row that represents the checked item in a table section.
In your view controller's -tableView:cellForRowAtIndexPath: delegate method, set the accessoryType of the cell to UITableViewCellAccessoryCheckmark if the cell's indexPath.row equals the selectedRow value. Otherwise, set it to UITableViewCellAccessoryNone.
In your view controller's -tableView:didSelectRowAtIndexPath: delegate method, set the selectedRow value to the indexPath.row that is selected, e.g.: self.selectedRow = indexPath.row
Another solution:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
[self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
return indexPath;
}
And yeah: you don't have to check if oldIndex is nil :)
Swift 3 and later:
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let oldIndex = tableView.indexPathForSelectedRow {
tableView.cellForRow(at: oldIndex)?.accessoryType = .none
}
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
return indexPath
}
swift:
var selectedCell:UITableViewCell?{
willSet{
selectedCell?.accessoryType = .None
newValue?.accessoryType = .Checkmark
}
}
then:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
Zyphrax suggested a great solution that worked great for me! And if you need to clear the previous selected row, just use:
[self.tableView reloadData];
in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
For swift
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
It should be
didHighlightRowAtIndexPath
instead of
tableView:didSelectRowAtIndexPath
Alex answer worked for me only after adding reload table ,
in .h
{
int selectedCell;
}
#property(nonatomic,readwrite)int selectedCell;
in .m
*cellForRowAtIndexPath*
if(indexPath.row == selectedCell)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
and in anywhere didHighlightRowAtIndexPath or didSelectRowAtIndexPath
self.selectedCell = indexPath.row;
[self.tableView reloadData];
If you want to use your custom image as accessoryType use below code
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"select_icon.png"]];
[imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)];
imgCheckMark.tag = 1000+indexPath.row;
NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow];
[self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone;
[self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark;
return indexPath;
}