IPhone: Add UIButton horizontally using scrollbar in View or TableView?

I am developing an application for the iPhone where I need to display UIButtons with reverse images horizontally (each row with two buttons with reverse images) with a scroll bar in the view. I have created a tab application. Please refer to the attached sample. How can I create such a screen? I need to create a tableview with ONE row only, where I can add UIButtons with horizontal inverse images (or) How can I achieve this easily? Is there an available program? Note. When I click on a button, I also need to know which button is pressed. I have actions for all buttons.

Please, help!

enter image description here

UPDATED:

I can get buttons with images horizontally (two buttons in a row). My problem is how can I mark it correctly so that I can fix it correctly.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[[UITableViewCell alloc]init]; static NSString *CellIdentifier1 = @"Cell"; cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; // if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease]; //} tableView.separatorStyle=UITableViewCellSeparatorStyleNone; cell.backgroundColor = [UIColor clearColor]; UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 80.0, 65.0)]; //UIImageView *imageView = [UIImage imageNamed:@"avatar.png"]; btn1.enabled = YES; // btn1.tag = ?????; // HOW can it tag it properly. [btn1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal]; [btn1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:btn1]; UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(180.0, 6.0, 80.0, 65.0)]; [btn2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; // btn2.tag = ?????; // HOW can it tag it properly as it is in horizontal position. [btn2 setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal]; [cell.contentView addSubview:btn2]; } -(IBAction) buttonPressed: (id) sender { UIButton *button = (UIButton *)sender; NSLog(@"Btn Tag: %d", [sender tag]); } 
0
source share
5 answers

NOW, you have to do it this way, at least you will know how many buttons there will be, the operation will be easy. in my code, I assume that I have 100 buttons, so, firstly, do not use a static pointer in the cells of the table view, using this code in this way.

NSString * CellIdentifier = [NSString stringWithFormat: @ "identifier_% d", indexPath.row]; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; // if (cell == nil) if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];

// now I assume that the button is 100, so I look at the index button before the first button and the pointer + 100 tags for another button, and the code looks like the button will be global or local, it depends on you.

and the function called by the button when they are pressed is equal to UIButton * Button = (UIButton *) sender; NSLog (@ "Selected Button Tag% d", Button.tag);

the tag that will print the value in 3 digits is the right button and the other is the left button, but I assume there are only 100 buttons, so check your logic.

Thanks,

0
source

I think you want to open the nib file (.xib) and add four UIButtons by dragging them to your view. Then you can add background images to them (first by adding images to your project, โ€œFile-> Add files to projectโ€ and then configure various states (selected, normal, disabled, selected) for your UIButtons. If they cause different IBActions, then you already know which button was pressed. If not, you can set the tag (for example, 1 or 2 or 3 or 4) in the Interface Builder for each button. Then in your IBAction check if sender.tag == 1, 2 , 3 or 4 to determine which button was pressed.

0
source

You should check out the Kirby Turner open source project, KTPhotoBrowser . Originally designed for photographs, but thumbnails are buttons that have a background image and are displayed in a grid.

0
source

Two ways to do this: you can put the table view and the scroll view, but in the scroll view, you need to maintain the size and the area in which the scroll view is used by the buttons. But in the table view, your work will be easy, you can just announce the button on the contents of the cells and assign the indexpath.row tag to the buttons and just hide the row of the table cell, it will look like a view scroll.i hope this helps

0
source

You need to make sure that you first indicate the color or background of the table view that you want to give and remove the rows of the table view, and this will give the feeling that you are in sight and not on the table view

 TableView.separatorStyle=UITableViewCellSeparatorStyleNone; 

and after that clean the cell using

 cell.backgroundColor = [UIColor clearColor]; 

and now the main thing is that you need to create a table this way

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"identifier_%d", indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //if (cell == nil) if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; UIButton *contactUsBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; contactUsBtn.frame = CGRectMake(220, 10, 20, 20); contactUsBtn.tag = indexPath.row; [contactUsBtn setBackgroundImage:[UIImage imageNamed:@"TroubleImg.png"] forState:UIControlStateNormal]; [contactUsBtn addTarget:self action:@selector(contactUsBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:contactUsBtn]; } return cell; } 

and the selector will like

 -(void)contactUsBtnAction:(UIButton*)sender { UIButton *Button = (UIButton *)sender; NSLog(@"Selected button tag is %d", Button.tag); } 

also check function

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"index path %d",indexPath.row); [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

I think this will help you a lot more.

0
source

All Articles