Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:22:25+00:00 2026-06-17T07:22:25+00:00

My table view uses a custom class to create cells MyCustomCell.h #import <UIKit/UIKit.h> @interface

  • 0

My table view uses a custom class to create cells

MyCustomCell.h

#import <UIKit/UIKit.h>

@interface MyCustomCell : UITableViewCell

@end

MyCustomCell.m

#import "MyCustomCell.h"
#import <QuartzCore/QuartzCore.h>


@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // Initialization code

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    self.selectionStyle = UITableViewCellSelectionStyleBlue;

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(5,4,80,80);
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 5.0f;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    float imgWidth =  self.imageView.image.size.width;
    if(imgWidth > 0)
    {
        self.textLabel.frame = CGRectMake(110,2,170,25);
        self.detailTextLabel.frame = CGRectMake(110,18,200,25);
        self.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
        self.detailTextLabel.alpha = 0.0;
        self.textLabel.font = [UIFont boldSystemFontOfSize:12];
        self.textLabel.textColor = [UIColor colorWithRed:.2314 green:.3490 blue:.5961 alpha:1.0];


    }
}

@end

and is use it like this:

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

    static NSString *CellIdentifier = @"Cell";


    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {


        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.accessoryType = UITableViewCellAccessoryCheckmark;



    }

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

    if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
    {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setBackgroundImage:[UIImage imageNamed:@"select2x.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(110, 25, 63, 32);
        //btn.tag = indexPath.row;

        viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewProfile.frame = CGRectMake(190, 25, 93, 32);
        //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
        [viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile@2x.png"] forState:UIControlStateNormal];
        [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:viewProfile];

    [cell.contentView addSubview:btn];

    }

    else
    {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(110, 25, 63, 32);
        //btn.tag = indexPath.row;

        viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewProfile.frame = CGRectMake(190, 25, 93, 32);
        //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
        [viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile.png"] forState:UIControlStateNormal];
        [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:viewProfile];

        [cell.contentView addSubview:btn];


       }

    return cell;

}

Now each cell contains two buttons which perform an action(I do not make action on cell tap but on button tap) as created like above

Now what i want is when a user taps a button(say select button) in a particular cell, that particular cell only should become highlighted or shaded or coloured and when user taps a button in another cell, that cell should become highlighted making the previous cell unhighlighted. I have tried storing previous and next indexpath’s in 2 different variables but not able to work on them to achieve what i want.

- (void)SelectButtonTapped:(UIButton *)button
{


    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;

    NSIndexPath *indexPath = [homeTable indexPathForCell:cell];

    // UITableViewCell *cell1 = [homeTable cellForRowAtIndexPath:indexPath];

    //currentSelection = indexPath.row;

    //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    [[homeTable delegate]tableView:homeTable didSelectRowAtIndexPath:indexPath];

     //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    /*if(currentSelection == 0)
    {
         [cell1 setSelected:YES animated:YES];
        currentSelection++;
    }
    else
    {
         [cell1 setSelected:YES  animated:YES];

    }*/
   // NSIndexPath *indexPath1 = [homeTable indexPathForCell:cell];

    NSLog(@"current===>%d",currentSelection);



    //[cell1 setSelected:NO animated:YES];

    //if(currentSelection == indexPath)




    //UIButton *clickButton = (UIButton *)sender;
    // NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:button.tag inSection:0];

    //button.tag = indexPathHighlight.row;





    //UITableViewCell *newCell = [homeTable cellForRowAtIndexPath:indexPathHighlight];


    //if(newCell.selected == NO)
    //{
    //[newCell setSelected:YES animated:YES];
     //   [newCell setBackgroundColor:[UIColor lightGrayColor]];
    //}
    //else
   // {
        //[newCell setSelected:NO animated:YES];
        //[newCell setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
    //}
}

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

    MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    cell1.selectionStyle= UITableViewCellSelectionStyleBlue;

}

You can look at my messy button action method. I have tried many options but with no success. Please help on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:22:26+00:00Added an answer on June 17, 2026 at 7:22 am

    Uncomment the line :

    btn.tag = indexPath.row+1000;
    

    Also change the cell selection style as

    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    

    when create it.

    In SelectButtonTapped method :

    - (void)SelectButtonTapped:(UIButton *)button
    {
      int buttonIndex = button.tag;
    
      for (int row = 0; row<numberOfRows; row++)
      {
        NSIndexPath indexPathHighlight = [NSIndexPath indexPathForRow:row inSection:0];
    
        UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight];
    
        if (row == buttonIndex - 1000)
        {
            [newCell setSelected:YES animated:YES];
        }
        else
        {
            [newCell setSelected:NO animated:YES];
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a table view which uses two types of cells
I'm using a table view that uses a custom UITableViewCell subclass, NoteCell . The
I have a UITableViewController that uses custom table view cells (and I have created
I built a table view, with Custom Table View Cells, where each cell has
I have a table view with numerous cells. When I press one, a tick
I have a table view with a custom cell ( UISwitch on every cell
I have seen that in Cocoa I can create a custom view using drawing
I have a split view-based app that presents a master-detail interface, and uses a
I have a view controller (EmbeddedMenuView) that uses a custom view (HorizontalMenuView). The Embedded
I have a UITableView that uses custom cells, loaded from nibs, and hooked up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.