I have a button on TableView Cell. My Issue is that on clicking on cell and button we want to perform same action. on clicking a cell i m using did select method. Is their any method on clicking a button on cell they perform same action as cell did select perform.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%ld CustumCell",(long)indexPath.row];
CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:2];
UIButton *button;
UIImageView *imageView;
UILabel *nameLabel;
if (screenWidth ==320) {
imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40, cell.contentView.frame.size.height+47)];
nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];
UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
[buttond setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
label.text=[cellNameArray objectAtIndex:indexPath.row];
label.textColor =[UIColor whiteColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
table.separatorStyle = UITableViewCellSeparatorStyleNone;
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
SelectVoucher *selectVoucher=[[SelectVoucher alloc]initWithNibName:#"SelectVoucher" bundle:nil];
selectVoucher.hotelName =[cellNameArray objectAtIndex:indexPath.row];
[self presentViewController:selectVoucher animated:YES completion:nil];
}
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:2];
UIButton *button;
UIImageView *imageView;
UILabel *nameLabel;
if (screenWidth ==320) {
imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40, cell.contentView.frame.size.height+47)];
nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];
UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
[buttond setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
//This line you need to add
[buttond addTarget:self action:#selector(YourMethodtocall:) forControlEvents:UIControlEventTouchDown];**
/////
UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
label.text=[cellNameArray objectAtIndex:indexPath.row];
label.textColor =[UIColor whiteColor];
}
And out side of cellForRowAtIndexPath, you can write this method like
-(IBAction)YourMethodtocall:(id)sender {
//your code here to perform action
}
May helps you,
Enjoy Coding !!
If you want to get the action of the UIButton that just copy past your code and it's working.But if you want to disable the UIButton so just button.userInteractionEnabled=NO and then access the didSelected Method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%ld CustumCell",(long)indexPath.row];
CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:2];
UIButton *button;
UIImageView *imageView;
UILabel *nameLabel;
if (screenWidth ==320) {
imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40, cell.contentView.frame.size.height+47)];
nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];
UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
[buttond setImage:[UIImage imageNamed:#"plus.png"] forState:UIControlStateNormal];
[buttond addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[buttond setTitle:[NSString stringWithFormat:#"%li %li",(long)indexPath.section,(long)indexPath.row] forState:UIControlStateDisabled];
UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
label.text=[cellNameArray objectAtIndex:indexPath.row];
label.textColor =[UIColor whiteColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
table.separatorStyle = UITableViewCellSeparatorStyleNone;
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
SelectVoucher *selectVoucher=[[SelectVoucher alloc]initWithNibName:#"SelectVoucher" bundle:nil];
selectVoucher.hotelName =[cellNameArray objectAtIndex:indexPath.row];
[self presentViewController:selectVoucher animated:YES completion:nil];
}
-(void)btnAction:(UIButton*)sender{
NSString *str=[sender titleForState:UIControlStateDisabled];
NSArray *ar=[str componentsSeparatedByString:#" "];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:[[ar objectAtIndex:1] intValue] inSection:[[ar objectAtIndex:0] intValue]];
btnSelectedSignOut= sender;
NSLog(#"Value from sign out action %#",indexPath);
//here is the indexpath value you can do any logic here.
}
The best thing is to disable the userInteractionEnabled of the button, which will direct all the touches to the UITableView's delegate method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath and you will be able to perform the cell touch events.
Alternatively, if at all you need to have the event of the button than set its userInteractionEnabled to YES and do add following code-
Inside cell creation method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
add
[buttond addTarget:self action:#selector(cellButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
add implement the event as
- (void) cellButtonTouched:(id)sender
{
}
You can define separate method for UIButton in UITableViewCell. if you want to perform separate action on clicking UIButton the didSelectRowAtIndexPath. kindly look over the code how to define separate action for UIButton.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
NSInteger index = indexPath.row;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"cell"owner:self options:nil];
cell = [nib objectAtIndex:0];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=indexPath.row;
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"cellButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0);
[cell.contentView addSubview:button];
}
return cell;
}
To define selected method.
-(void)aMethod:(UIButton*)sender
{
NSLog(#"I Clicked a button %d",sender.tag);
}
Related
I'm creating a UISwitch programmatically in one of the tableview datasource function(cell for row at index). when I'm scrolling the table view, the OFF state switches are malfunctioned(UI) two rounds appeared. Attaching the screenshot of the switch.
Appreciate your help!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60, (cell.contentView.frame.size.height/2)-20 , 100, 100)];
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
[cell.contentView addSubview:myswitch];
myswitch.tag = indexPath.row;
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have faced the same issue and fixed with following code before creating switch
for(UIView *subView in cell. contentView.subviews){
if ([subView isKindOfClass:[UISwitch class]]) {
[subView removeFromSuperview];
}
}
Or
static NSString *TableIdentifier = #"YourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
//place your all UI elements code here.
}
I have updated your code, try this will work for you,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
if(mySwitch == nil){
myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
[cell.contentView addSubview:myswitch];
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
}
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
myswitch.tag = indexPath.row+597; //597 is extra number added to tag
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I need to create a table view with some buttons as its elements, view gets loaded fine, but while scrolling down buttons gets overlapped. I think old buttons are not cleared and new buttons are placing over it. Please help me to fix this problem. Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
return cell;
}
just replace the line
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
is now
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
(or) //use this in cell for rowatindexpath
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if(cell == nil)
//{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
//}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
}
return cell;
}
If you use Xib you can use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
I'd suggest using tags for this. See the code below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
NSLog(#"index path %ld",(long)indexPath.row);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSInteger buttonTag = 100500;
UIButton *button = [cell.contentView viewWithTag: buttonTag]
if(button == nil)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 160.0, 25);
[cell.contentView addSubview:button];
}
[button setTitle:[NSString stringWithFormat:#"data %ld",(long)indexPath.row] forState:UIControlStateNormal];
return cell;
}
Please insert below code just before you add button as subview, it will remove all the buttons in your content view of cell before you add a new one.
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}
I know that there are many duplicates here regarding this question but my requirement is that I added 2 UIButtons on one cell and both the buttons will open two different views. If I set the property userInteractionEnabled to YES, then it will not pic the 'finalID' from didSelectRowAtIndexPath from the below code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableViewProject){
static NSString *cellId = #"attachmentCellId";
attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"attachmentCell" owner:self options:Nil];
for(id object in nib)
{
if([object isKindOfClass:[attachmentCell class]])
{
cell = (attachmentCell *)object;
break;
}
}
UIButton *button;
button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
[button addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
button.userInteractionEnabled = YES;
//button.userInteractionEnabled = NO;
[cell.contentView addSubview:button];
UIButton *buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
[buttonAttach addTarget:self action:#selector(buttonAttachClicked) forControlEvents:UIControlEventTouchUpInside];
buttonAttach.userInteractionEnabled = YES;
//buttonAttach.userInteractionEnabled = NO;
[cell.contentView addSubview:buttonAttach];
cell = [nib objectAtIndex:0];
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:#"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:#"%d", attach.attachment];
cell.date.text = attach.date;
}
return cell;
}
And my DidSelectRowAtIndexPath is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Array == %#", anotherTempArray);
NSString *finalId = [NSString stringWithFormat:#"%#", [anotherTempArray objectAtIndex:indexPath.row]];
NSLog(#"final id for selected row = %#", finalId);
NSUserDefaults *defaultForFinalId = [NSUserDefaults standardUserDefaults];
NSString *setFinalId = finalId;
[defaultForFinalId setObject:setFinalId forKey:#"SETFINALID"];
if(tableView == self.tableViewProject)
{
[self buttonClicked];
//[self viewDidLoadForList];
}
if(tableView == self.tableViewAttachmentList)
{
[self buttonAttachClicked];
}
}
If you want to call the selector of UIButton inside cell, then what you dont need to use didSelectRowAtIndexPath method.
What you did was correct upto adding handler to UIButton. Now, remove your didSelectRowAtIndexPath code to button's click handler. Here is how you can get indexPath from the button click handler.
- (void)buttonClicked:(UIButton *)sender {
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview; //Since you are adding to cell.contentView, navigate two levels to get cell object
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Now you can do the code you put in didSelectRow here.
}
Hope this helps.
change your code for button selector
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonClicked
[buttonAttach addTarget:self action:#selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonAttachClicked
If U use buttons than U don't need to use didSelectRowAtIndexPath. Set tags on buttons to match indexPath.row of the cell in cellForRowAtIndexPath... This way when U press the button in buttonAttachClicked or buttonClicked just check the senders tag and pull that cell from the UITableView
Use this code for cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableViewProject){
static NSString *cellId = #"attachmentCellId";
attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"attachmentCell" owner:self options:Nil];
for(id object in nib)
{
if([object isKindOfClass:[attachmentCell class]])
{
cell = (attachmentCell *)object;
break;
}
}
cell.button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
[cell.button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.button];
cell.buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
[cell.buttonAttach addTarget:self action:#selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.buttonAttach];
cell = [nib objectAtIndex:0];
}
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:#"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:#"%d", attach.attachment];
cell.date.text = attach.date;
[cell.button setTag:indexPath.row];
[cell.buttonAttach setTag:indexPath.row];
return cell;
}
I used BVReorderTableView https://github.com/bvogelzang/BVReorderTableView to reorder row in tableview. But i have a problem: When I add UIButton to each row, this button always show clear color.Once selected row, this button just shown.
I tried this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[prioritybtn setFrame:CGRectMake(220, 0, 70, 32)];
[prioritybtn setTag:4000];
[cell.contentView addSubview:prioritybtn];
}
UIButton *priorityButton = (UIButton*)[cell.contentView viewWithTag:4000];
if ([[items objectAtIndex:indexPath.row] isKindOfClass:[NSString class]] &&
[[items objectAtIndex:indexPath.row] isEqualToString:#"DUMMY"]) {
cell.textLabel.text = #"";
cell.contentView.backgroundColor = [UIColor clearColor];
prioritybtn.hidden= YES;
}
else {
cell.textLabel.text = [items objectAtIndex:indexPath.row];
[priorityButton setSelected:NO];
[priorityButton setTitle:#"Priority" forState:UIControlStateNormal];
priorityButton.enabled = YES;
}
return cell;
}
Also, I want to hide button only at first row, other row is still shown. How can i do that? Thanks
To hide the button at first row:
if (indexPath.row == 1){
prioritybtn.hidden = YES;
prioritybtn.userInteractionEnabled = NO;
} else {
prioritybtn.hidden = NO;
prioritybtn.userInteractionEnabled = YES;
}
Add this before return cell;
Regarding the color, add this where you initialize the button:
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
prioritybtn.backgroundColor = [UIColor greenColor]; //put the color you want
Try like this:-
cell.selectionStyle =
UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
UIButton *reletedbtn=nil;
UILabel *Nameoflb=nil;
cell = [tableeample dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Nameoflb = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,40)];
Nameoflb.backgroundColor=[UIColor clearColor];
Nameoflb.textAlignment=NSTextAlignmentCenter;
Nameoflb.textColor=[UIColor blackColor];
[Nameoflb setFont:[UIFont fontWithName:#"Helvetica-Bold" size :14]];
Nameoflb.text=[dataarray objectAtIndex:indexPath.row];
[[cell contentView] addSubview:Nameoflb];
if (indexPath.row!=0)
{
reletedbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
reletedbtn.frame=CGRectMake(Nameoflb.frame.origin.x+Nameoflb.frame.size.width, Nameoflb.frame.origin.y+10,40, 25);
reletedbtn.tintColor=[UIColor redColor];
[reletedbtn setTitle:#"butoon" forState:UIControlStateNormal];
//[reletedbtn addTarget:self action:#selector(statusMasseage:) forControlEvents:UIControlEventTouchUpInside];
//reletedbtn.tag=1;
[[cell contentView] addSubview:reletedbtn];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I have table view in side bar which is similar to facebook app. In my sidebar tableview i have imageview in each row. I want the imageview will be changed when i clicked the cell and it will be retain while for selecting another cell. Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}
Try this in your didSelectRowAtIndexPath delegate method :-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"yourImage"];
Try this in cellForRowAtIndexPath.
UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image"]];
cell.selectedBackgroundView = selectedImageView;
Need to capture the selected indexpath in didSelectRowAtIndexPath: method and need to reload the tableview in that method. In cellForRowAtIndexPath need to check the selected index with current index.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
[cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}