how to add the UITableView detailDisclosurebutton custom in ipad app - ios

I have tableView with detailDisclosure button it works fine but i want to give title to detail disclosure button so i think i need to add custom UIButton for detailDisclosure is it possible to do like i want to do this without using custom cell.
If i want to give image it works fine but how may i give title to this button like comment.
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"loginN.png" ]];

Try this..
UIButton *btn = [[[UIButton alloc]init]autoerelease];
btn .frame = CGRectMake(210.0, 0.0, 90, 25);
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"loginN.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubView: btn];
and in btnClickHandler do this to get indexPath
- (void)btnClickHandler :(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Get your data source object from indexPath.row.
}

Why don't u set the a button as accessory view just like u try setting an image..??
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);//Set the frame u need for the view
//Customize the button
[button setTitle:#"title" forState:UIControlStateNormal];
//And set it as accessory view
cell.accessoryView = button;
Here is the result...

Related

uibutton background image not changing objective c

I have table view with custom cell class.
Inside cell I've five uibuttons and in its custom class I've made outlets for all of them and have only one function to handle their tap events like this.
- (IBAction)starClicked:(id)sender {
UIButton *btn = (UIButton*)sender;
switch (btn.tag){
case 501:
self.starOne = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starOne setBackgroundImage:[UIImage imageNamed:#"day-time-temp-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
[self.starOne setTitle:#"23" forState:UIControlStateNormal];
[self.starOne setNeedsLayout];
self.starTwo = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starTwo setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starThree = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starThree setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFour = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFour setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFive = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFive setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
break;
...
}
}
by using tag property I am checking which button is tapped, and it is working fine as I have checked it using break point. and when I press first button I want to change image of first button. And on second button I want to change image of first and second button. It is like a rating control. I want to change star image with highlighted image of star.
but below code not working. I have tried setting setNeedLayout too. But image is not changing.
Edit
this is my cell for row function.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuestionsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"questCell"];
if(cell == nil){
cell = [[QuestionsCell alloc] init];
}
[cell customizeCellWithQuestions:[self.questionList objectAtIndex:indexPath.row] textSize:1 index:indexPath.row];
return cell;
}
This is my customizeCellWithQuestions function inside my QuestionCellClass.
-(void)customizeCellWithQuestions:(Question *)question textSize:(float)textSize index:(NSUInteger)index{
[self.questionTitleTv setText:[question.question capitalizedString]];
[self.questionIndexLbl setText:[NSString stringWithFormat:#"%lu",(unsigned long)index+1]];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor colorWithRed: 213.0/255.0 green: 213.0/255.0 blue: 213.0/255.0 alpha: 0.0];
}
You need to set Image for one state individually.
Using OR operator won't work
[self.starOne setBackgroundImage:[UIImage imageNamed:#"day-time-temp-icon.png"] forState:UIControlStateNormal];

Adding a delete button to UiTableViewCell

Since I am using a slide-out menu controller in my app - the swipe to delete on a UITablewViewCell no longer works as the pan gesture is used to open / close the side menu.
So, I was thinking about adding a delete button to show up on each cell all the time - so user can just tap delete to remove the cell.
I added this code to UItableView cellforRowAtIndexPath method:
/* Remove Button */
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:#"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:#selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
This adds the button and in the remove method I am not sure how to go about actually removing the correct cell.
Can anyone point me in the right direction here?
Thanks!
Basically you need index for the cell which has to be deleted when delete is pressed.
you can set the tag property and when button is pressed you can check the tag propery of the button which is sending event.
see below code,
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeButton.tag = indexPath.row;
removeButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
[removeButton setTitle:#"Remove" forState:UIControlStateNormal];
removeButton.tintColor = [UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]; /*#aaaaaa*/
removeButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cell addSubview:removeButton];
[removeButton addTarget:self
action:#selector(removeItem:)
forControlEvents:UIControlEventTouchUpInside];
-(void) removeItem:(id) sender
{
UIButton *button = (UIButton*)sender;
int index = button.tag;
}
Try this,
- (IBAction)removeItem:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
//Remove the cell at indexPath
}
//1. add tag to the button, for identifing later
removeButton.tag = indexPath.row;
//2. Get the cell
UITableViewCell *cellToBeDeleted = [tableView cellForRowAtIndexPath:sender.tag];

How to select single button on UITableView

I have an issue in an UITableView as I need to place a button on each row of the tableview and if i click the button, it gets selected at a time and selecting is like toggle. How to achieve this, please help me out to solve this problem.
Below is the following code that am trying to get it out. I am creating button on cellForRowAtIndexPath as shown below:
UIButton *Btn = [[UIButton alloc]init];
Btn .frame = CGRectMake(3, 10, 33, 30);
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
Btn.tag=indexPath.row;
[cell.contentView addSubview:Btn];
and the method: for button action:
-(void)method:(UIButton*)sender
{
NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Btn = (UIButton *)sender;
//Btn.tag=sender.tag;
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
}
You have an array to store whether the button of a cell is selected or not. In the cellForRowAtIndexPath method, you should set 2 images for 2 states UIControlStateNormal and UIControlStateSelected of the button:
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateSelected];
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
Then in the method: function, you switch the state of the button via Btn.selected based on the value in the array.
UIImage * btnLinkNormal = [UIImage imageNamed:#"select.png"];
[cell.linkButton setImage:btnLinkNormal forState:UIControlStateNormal];
// Add image to button for pressed state
UIImage * btnLinkHighlighted = [UIImage imageNamed:#"deselect.png"];
[cell.linkButton setImage:btnLinkHighlighted forState:UIControlStateHighlighted];

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

UITableView subview to uitableviewcell

i am trying to add a button to the tableview and no matter what frame i specify the button is not visible. This is a custom UITableViewCell and I am adding the button in the code rather than in the xib file is so that I can click on the button and perform an action which is not didSelectRowAtIndexPath:
I have the following code in cellForRowAtIndexPath: What am I missing?
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = bframe;
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
try this
UIButton *button = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];

Resources