I have a dynamically created UIButton in my table's cellForRowAtIndexPath: method. It loads fine, and it can be selected, but it doesn't do the highlight animation when it is selected. I tried adding:
if(commandButton.isHighlighted), and that didn't trigger when selected. Here's how I load the cell:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* CellIdentifier=#"CommandCell";
UITableViewCell* commandCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIButton* commandButton = [[UIButton alloc]initWithFrame:commandCell.frame];
[commandButton setTitle:#"Title" forState:UIControlStateNormal];
[commandCell.contentView addSubview:commandButton];
commandButton.backgroundColor = [UIColor blackColor];
return commandCell;
}
Whenever I click on it, commandButton is not highlighted...
EDIT: it was a stupid problem, I wasn't holding down my mouse for long enough. For those of you who have a similar problem, simply press down on the button for longer in the iOS simulator.
may this help you add the bellow code into ur code
if you want to so some image on tuchupinside then below code.
[commandButton setBackgroundImage:/*add you image using UIImage*/forState: UIControlStateHighlighted];
if you want simple (default background color when it appear in button on tuchupinside).
UIButton *buttonTap=[UIButton buttonWithType:UIButtonTypeRoundedRect];
and set the frame like this
buttonTap.frame=commandCell.frame;
//make some adjustment as per requied.
if u want some blur type effect(semi partial on selection )
Then you go for
UIButtonTypeCustom //type
The UIButton that you create in the following line
UIButton* commandButton = [[UIButton alloc]initWithFrame:commandCell.frame];
is a custom styled UIButton by default. For this button, you will have to set all the properties manually. What you want is this:
UIButton *commandButton = [UIButton buttonWithType:UIButtonTypeSystem];
[commandButton setFrame:cell.frame];
The UIButtonTypeSystem contains the style that you are looking for.
Hope this helps!
I don't know where are using this ? but still my suggestion is to give a tag to the button if you are adding it to the cell like :
UIButton* commandButton = [[UIButton alloc]initWithFrame:commandCell.frame];
[commandButton setTitle:#"Title" forState:UIControlStateNormal];
commandButton.tag = 1;
[commandCell addSubview:commandButton];
commandButton.backgroundColor = [UIColor blackColor];
and while accessing the same button ;
//you have to get the cell where button has been clicked
UIButton *button =(UIButton *)[cell viewWithTag:1];
and on this button you can use :
if(commandButton.isHighlighted)
Before you add button to cell view, try to add:
NSArray * views = [commandCell.contentView subviews];
for (UIView *object in views) {
[object removeFromSuperview ];
}
Because maybe u have multiple cells one on other, so the button is actually clicked is the "most bottom cell view" button.
Related
I need to implement the uitableview selection with the design similar to the underseen design.
Please let me know how to accomplish this.
The best way for implementing checkbox is, you can take button and toggle the image.UIButton is a subclass of UIController,which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox
Create a custom class of UITableview cell
Add number of buttons you wants on the cell
Give class name of UITableViewCell to the cell of tale view
Connect IB_Outlates of Buttons
In the delegate cellForRowAtIndexpath
myButton.tag = indexPath.row
[cell.myButton addTarget:self action:#selector(shippingBtnAction:) forControlEvents:UIControlEventTouchUpInside];
- (void)shippingBtnAction:(id)sender
{
UIButton* btn = (UIButton*)sender;
btn.selected = ! btn.selected;
if (btn.isSelected)
{
_isDefaultShipping = 1;
[self.shippingBtn setImage:[UIImage imageNamed:#"check"] forState:UIControlStateNormal];
}
else
{
_isDefaultShipping = 0;
[self.shippingBtn setImage:[UIImage imageNamed:#"uncheck"] forState:UIControlStateNormal];
}
}
Actually i am create action for custom accessory view uibutton in cellForRowAtIndexPath
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.frame = CGRectMake(0, 0, 25, 25);
[addButton setBackgroundImage:[UIImage imageNamed:#"Unchecked.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)checkButtonTapped:(id)sender event:(id)event{
[self tableView: self.grouptableview accessoryButtonTappedForRowWithIndexPath: indexPath];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSLog(#"indexPath.row: %ld",(long)indexPath.row);
}
its working fine when i didn't use the didSelectRowAtIndexPath method.
Problem is: some times the click event in accessory view not recognize instead of it triggers the didSelectRowAtIndexPath function.
How to i specify the user interaction for accessory view and differentiate from the didSelectRowAtIndexPath in tableview ?
please help me to solve this. Thanks..
It looks like design Problem.
(0, 0, 25, 25)
get a new image from your designer with more transparent area towards top, down, left and right.
It will solve your problem.
Thanks.
Your problem is the height and width of your uibutton which you are specifying in your program. function CGRectMake defines the position and the area your button covers which is small and enables a user to perform its functions like touchupinside touchupoutside etc.
Sometimes user touch over a button but at that time cell gets focus and executes accordingly because of uibutton cover area over it.
I have a UICollectionView with its cells all laid out.
I have this declared as a subview:
#property (nonatomic, strong) UIButton *aButton;
I then have that declared in each cell like so:
if (_aButton == nil)
{
_aButton = [UIButton buttonWithType:UIButtonTypeSystem];
}
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
The button click method is like so:
- (IBAction) aButtonPressed:(UIButton *) sender
{
// Code never gets heree
}
The if(_aButton== `nil) is needed since cells get reused.
How do I make this work now? Thanks.
Add button action code before button added to view.... May be it will work .
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
I think, the way you are initializing the button is going to give you frame as 0,0,0,0 . So first give it a proper frame and title to be visible on the screen.
Then try tapping on that title and see if it works or not.
It's unclear what you trying to achieve here, but the button is not working because you not setting it's position and size on the cell. It could be done by setting frame or layout constraints.
Try to set button frame:
_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
You can also set background color for the button, just to make it visible on the cell:
_aButton.backgroundColor = [UIColor redColor];
Everything else is correct, button should work.
I don't seem to be able to find anyone that has the same issue as me. All my tableview cells are perfectly fine for accessibility but my headers don't seem to. I have a button in my header view which just won't work for accessibility... Can anyone help me out with this?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [[[UIButton alloc] initWithFrame: CGRectMake(282, 10, 28, 28)] autorelease];
button.isAccessibilityElement = TRUE;
button.titleLabel.text = #"Informatie";
[button addTarget:self action:#selector(infoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
}
I realise that I paraphrased this horribly. What I mean is, the button is working and all, just not for users that have assistive touch enabled on their phones. The phone isn't 'showing up' with the voice that helps the disabled users out.
And I do get the log error "AX ERROR: Could not find my mock parent, most likely I am stale."
You shouldn't set the button's title by modifying the label's text directly. Instead, use setTitle:forState:, e.g.:
[button setTitle:#"Informatie" forState:UIControlStateNormal];
Also, buttons are typically created with the class method buttonWithType: instead of alloc/initWithFrame:.
I am trying to access the properties of the UITableViewCell of a QuickDialog form.
More specifically, I am trying to access the accessoryView property of a QEntryElement (QDateTimeInlineElement), which is "hidden" in the property list of the object I create.
I am trying to access the cell using
UITableViewCell *thisCell = [dateelement getCellForTableView:self.quickDialogTableView controller:self];
But for some reasons nothing is displayed. I am trying to insert a UIButton in it, like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"=" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 24, 24);
and then
thisCell.accessoryView = button;
Am I accessing the property in a wrong way or maybe the button is not created at all? No errors are displayed, but the accessoryView is empty.
Thank you in advance
This issue looks very close to what you are asking. Basically, you first have to provide your own QuickDialogStyleProvider, implementing the cell:willAppearForElement:atIndexPath: method the way escoz suggests there:
Once you get the call in the provider method, you have full control of the cell. You can just check if the cell is of type QEntryTableViewCell, and if it is, cast to that type and change the color/font of the textField property. A nice side effect is that this will also change the color for all subclasses, like the radio button, date/time fields, etc..
So, in your case you'd do something like
- (void)viewDidLoad
{
self.quickDialogTableView.styleProvider = self;
}
- (void)cell:(UITableViewCell *)cell willAppearForElement:(QElement *)element atIndexPath:(NSIndexPath *)indexPath
{
if([cell isKindOfClass:[QDateTimeInlineElement class]])
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"=" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 24, 24);
cell.accessoryView = button;
}
}
Sorry if it's not fully correct, I'm away from Xcode at the moment.