In my Last Question extension Now I am able to select rows in a particular section but How section will select using buttons
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
Header *headerView = [tableView dequeueReusableCellWithIdentifier:#"HeaderView"];
UILabel *name = (UILabel*) [headerView.contentView viewWithTag:section+2];
UILabel *code = (UILabel*) [headerView.contentView viewWithTag:section+4];
name.text = [_data[section] valueForKey:#"Name"] ;
code.text=[_data[section] valueForKey:#"Code"] ;
imagebutton=(UIButton*)[headerView.contentView viewWithTag:section+3];
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
if(headerArray.count>0)
{
if([headerArray containsObject:#"0"])
{
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
}
else
{
UIImage *btnImage = [UIImage imageNamed:#"ic_floating_done_#1x"];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
}
}
UIButton *btn=(UIButton*)[headerView.contentView viewWithTag:section+1];
[btn addTarget: self
action: #selector(buttonClicked:event:)
forControlEvents: UIControlEventTouchUpInside];
return headerView;
}
This is My header view here I am using button to select section
(void)buttonClicked:(id)sender event:(id)event
{
if(imagebutton.currentImage == [UIImage imageNamed:#""] )
{
UIImage *btnImage = [UIImage imageNamed:#"ic_floating_done_#1x"];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
}
else
{
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
}
}
TableView CheckMarks
For details, I am giving my last question link here my data and tableview delegates available
My problem is I am not able to select the first section if I click any row two sections are selected
If particular section row selects that section only select and if section select all rows will select.
Please give a clear example for this.
Try to connect the header UIView to a property in your .h file.
Related
I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.
I was able to add a "setbackgroundimage" to my UIButton in my array. Now all buttons have the same image. How do I create a switch statement to be able to set a different image for each initwithobjects #"" in my NSarray?
Below is the code I'm using:
menuItems = [[NSArray alloc]initWithObjects:#"",#"",#"",#"",#"",#"",#"", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
//mybutton.backgroundColor = [UIColor redColor];
[mybutton setBackgroundImage:[UIImage imageNamed:#"menubutton.png"]
forState:UIControlStateNormal];
[mybutton setTag:b];
[mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
[mybutton setSelected:false];
[mybutton addTarget:self action:#selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
[m_scrollview addSubview:mybutton];
originofButtons += (buttonHeight + buttonseparator);
}
To set the image for selected:
[mybutton setBackgroundImage:[UIImage imageNamed:#"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
Whereas you want to set different each, you can link tag .
switch (myButton.tag) {
case 0:
[mybutton setBackgroundImage:[UIImage imageNamed:#"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
break;
case 1:
[mybutton setBackgroundImage:[UIImage imageNamed:#"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
break;
}
This can be help you for your scenario.
You can add your button image name in NSArray and then set image for each case using the below code snippet:
NSArray* menuItems = [[NSArray alloc]initWithObjects:#"FirstScreen",#"SecondScreen",#"ThridScreen", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]init ];//WithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
[mybutton setBackgroundImage:[UIImage imageNamed:[menuItems[b] stringByAppendingString:#".png"]] forState:UIControlStateSelected];
i am hoping this will be helpful to you according to your requirements.....
menuItems = [[NSArray alloc]initWithObjects:#"",#"",#"",#"",#"",#"",#"", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
//mybutton.backgroundColor = [UIColor redColor];
[mybutton setBackgroundImage:[self getImageFromIndex:b %4]
forState:UIControlStateNormal];
[mybutton setTag:b];
[mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
[mybutton setSelected:false];
[mybutton addTarget:self action:#selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
[m_scrollview addSubview:mybutton];
originofButtons += (buttonHeight + buttonseparator);
}
-(UIImage *) getImageFromIndex:(short) index
{
switch (index) {
case 0:
return [UIImage imageNamed:#"card_01.png"];
break;
case 1:
return [UIImage imageNamed:#"card_02.png"];
break;
case 2:
return [UIImage imageNamed:#"card_03.png"];
break;
case 3:
return [UIImage imageNamed:#"card_04.png"];
break;
default:
return [UIImage imageNamed:#"card_01.png"];
break;
}
return nil;
}
In UITableView cellForRowAtIndexPath create one button with one image. write action for that button click to change the button images. here the code.
ButtonImage = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
- (IBAction)select_id:(id)search
{
UIButton *tempBtn =(UIButton *) search;
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
clicked the button images changed(work_exe.png). wen click the same button again means the image want to change (work_exe_old.png) its like check box in tableview. how can i achieve tis
Instead of doing it in this way you can customise your code much better. Check the code below:
ButtonImage = [UIImage imageNamed:#"work_exe.png"];
ButtonImageSelected = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
And you can implement this selector method like this:
-(IBAction)select_id:(UIButton *) tempBtn {
[tempBtn setSelected:! tempBtn.selected];
}
set the uibutton's background images for each of its control states,
e.g.
UIImage *normal = [UIImage imageNamed:#"work_exe.png"];
UIImage *highlighted = [UIImage imageNamed:#"work_exe_old.png"];
UIImage *selected = [UIImage imageNamed:#"work_exe_old.png"];
[myButton setBackgroundImage:normal forState:UIControlStateNormal];
[myButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];
[myButton setBackgroundImage:selected forState:UIControlStateSelected];
If you want do a action by clicking on UITableViewCell you have to implement the didSelectRowAtIndexPath and before decalre int i; gobally then follow the below code to make the changes for Button image
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
switch (i) {
case 0:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=1;
break;
case 1:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe0.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe1.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe2.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=0;
break;
default:
break;
}
[yourtablename reloadData];
note: write this code after the cellForRowAtIndexPath method
I am not sure weather I understand you right. If you want the image to change after a click, you should save the current state of your image somewhere, e.g. in an NSInteger that holds the state for your cell as a number that represents which image should be shown. In the tableView:cellForRowAtIndexPath: method, put the right image (according to your NSInteger) into the cell.
#interface MyTabelViewClass()
#property (assign) NSInteger currentImageState;
...
#end
In the tableView:cellForRowAtIndexPath: method, you somewhere have to decide which image to chose, according to this property:
switch (self.currentImageState) {
case 0:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
case 1:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
...
}
In the tableView:didSelectRowAtIndexPath you have to change the state somewhere, like
self.currentImageState = (self.currentImageState + 1) % maximumNumberOfStates;
...
[self.myTableView reloadData]
...
Maybe it's a good idea to call reloadData with some delay because you want to wait for the deselection animation to finish first. You can do this by calling
[self.myTableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.35]
Hi guys I'm a beginner in IOS development. I have created a tableview with 3 buttons in each row. And I have coded a method for one of the buttons. It turns out the button only works in the last row. Please help me with this.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSInteger row = [indexPath row];
......
startButton = [UIButton buttonWithType:UIButtonTypeSystem];
pauseButton = [UIButton buttonWithType:UIButtonTypeSystem];
stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
[pauseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"tianlanse.png"] forState:UIControlStateNormal];
[pauseButton setBackgroundImage:[UIImage imageNamed:#"lianglv.png"] forState:UIControlStateNormal];
[stopButton setBackgroundImage:[UIImage imageNamed:#"qianhong.png"] forState:UIControlStateNormal];
startButton.frame = CGRectMake(522.0f, 26.0f, 65.0f, 50.0f);
pauseButton.frame = CGRectMake(443.0f, 26.0f, 65.0f, 50.0f);
stopButton.frame = CGRectMake(603.0f, 26.0f, 65.0f, 50.0f);
stopButton.hidden = YES;
pauseButton.hidden = YES;
startButton.tag = indexPath.row+1001;
pauseButton.tag = indexPath.row+2002;
stopButton.tag = indexPath.row+3003;
[startButton setTitle:#"Start" forState:UIControlStateNormal];
[cell addSubview:startButton];
[pauseButton setTitle:#"Pause" forState:UIControlStateNormal];
[cell addSubview:pauseButton];
[stopButton setTitle:#"Stop" forState:UIControlStateNormal];
[cell addSubview:stopButton];
}
[startButton addTarget:self action:#selector(StartbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
......
}
-(IBAction)StartbuttonClicked:(id)sender{
NSUInteger row = ((UIButton *)sender).tag-1001;
NSLog(#"start button clicked,Row:%lu",(unsigned long)row);
[stopButton viewWithTag:((UIButton *)sender).tag+2002].hidden = NO;
[pauseButton viewWithTag:((UIButton *)sender).tag+1001].hidden = NO;
[startButton viewWithTag:((UIButton *)sender).tag].hidden = YES;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return tableCellCount;//tableCellCount is set to be 4 in viewdidload
}
Only in the last row, the button works. I tested the tag for each button. It's valid;
2014-02-09 15:38:31.919 Weight Training[336:60b] 1001,2002,3003
2014-02-09 15:38:31.921 Weight Training[336:60b] 1002,2003,3004
2014-02-09 15:38:31.923 Weight Training[336:60b] 1003,2004,3005
2014-02-09 15:38:31.925 Weight Training[336:60b] 1004,2005,3006
You should create buttons for each row in a tableView programmatically or create custom cell via storyboard.
First way:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSInteger row = [indexPath row];
......
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
[startButton addTarget:self action:#selector(startButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeSystem];
[pauseButton addTarget:self action:#selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
[stopButton addTarget:self action:#selector(stopButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[pauseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"tianlanse.png"] forState:UIControlStateNormal];
[pauseButton setBackgroundImage:[UIImage imageNamed:#"lianglv.png"] forState:UIControlStateNormal];
[stopButton setBackgroundImage:[UIImage imageNamed:#"qianhong.png"] forState:UIControlStateNormal];
startButton.frame = CGRectMake(522.0f, 26.0f, 65.0f, 50.0f);
pauseButton.frame = CGRectMake(443.0f, 26.0f, 65.0f, 50.0f);
stopButton.frame = CGRectMake(603.0f, 26.0f, 65.0f, 50.0f);
stopButton.hidden = YES;
pauseButton.hidden = YES;
startButton.tag = indexPath.row+1001;
pauseButton.tag = indexPath.row+2002;
stopButton.tag = indexPath.row+3003;
[startButton addTarget:self action:#selector(StartbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
......
}
Then create 3 function:
- (void) startButtonClicked:(id) sender{
}
- (void) stopButtonClicked:(id) sender{
}
- (void) pauseButtonClicked:(id) sender{
}
If something doesn't work or if you need more information let me know.
What I'm trying to do is place a UILabel on top of a UIButton.
The UIButton have an image as a background and the result is I cannot see the normal text on the UIButton, therefore I think to place the UILabel on top of it.
My code is:
NAlertView *customAlert = [[NAlertView alloc] initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"cancel", nil];
[customAlert show];
NAlertView.m:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]]) // Override UIImageView (Background)
{
UIImageView *imageView = obj;
[imageView setImage:[UIImage imageNamed:#"CustomAlertView"]];
[imageView setAlpha:0.7];
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor clearColor];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_gold_on.png"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_black_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_black_on.png"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
Result:
That means you just want to add Title on UIButton?
Try This -
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:#"Title" forState:UIControlStateNormal];
Replace
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
to
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
change all such possibilities.
and use the below method to change the text of the button
[button setTitle:#"text" forState:UIControlStateNormal];
Hope it helps.