How to open a link with a UIButton within a UITableView - ios

I have created the below code to display buttons in UITableView. I want to add click event for every particular cell in table to reach to google links by clicking into that button.
How to add to clicking events into the buttons options for particular cell and to reach google link?
- (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];
}
UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(5,5,40,40)];
[button addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"lori"] forState:UIControlStateNormal];
[button setTag:9001];
[cell addSubview:button];
[cell setIndentationWidth:45];
[cell setIndentationLevel:1];
cell.textLabel.text = [thearray objectAtIndex:indexPath.row];
return cell;
}
-(void) buttonpressed:(UIButton *)sender {}

Try something like:
-(void) buttonpressed:(UIButton *)sender {
NSString* launchUrl = #"http://apple.com/";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
}
this will open safari pointing to the URL you pass in.
If you do not want to open safari, then you should display your own UIWebView.
Furthermore, you might want to add your button to contentView:
[cell.contentView addSubview:button];

Related

iOS Custom Cell - Reuse identifier

I am trying to create a custom cell with favorite button on it. Its working fine but I think reuse identifier is creating problem here. I have tried it in two ways and it is working fine in both.
Here is my first try:
When I am selecting the button, it is changing to yellow button and vice -versa. But after scrolling up or down,it is changing to its normal state again. Why?
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CI=#"CELL";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
if(!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
}
favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];
[cell.contentView addSubview:favbtn];
[favbtn setBackgroundImage:[UIImage imageNamed:#"starr.png"] forState:UIControlStateNormal];
[favbtn addTarget:self action:#selector(fav:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
return cell;
}
-(void)fav:(id)sender
{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"starr.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
Here is my second try:
Here every sixth cell is showing the same state.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CI=#"CELL";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
if(!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];
[cell.contentView addSubview:favbtn];
[favbtn setBackgroundImage:[UIImage imageNamed:#"starr.png"] forState:UIControlStateNormal];
[favbtn addTarget:self action:#selector(fav:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
}
return cell;
}
First of all modify this methods:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CI=#"CELL";
UIButton * favbtn;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
if(!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];
[cell.contentView addSubview:favbtn];
[favbtn setTag:11221133];
}
favbtn = [cell.contentView viewWithTag:11221133];
[favbtn setBackgroundImage:[UIImage imageNamed:#"starr.png"] forState:UIControlStateNormal];
[favbtn setImage:[UIImage imageNamed:#"starr.png"] forState:UIControlStateNormal];
[favbtn setImage:[UIImage imageNamed:#"star.png"] forState: UIControlStateSelected];
//Check selections here and if this index is selected make btn selected.
[favbtn addTarget:self action:#selector(fav:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
cell.contentView.tag = indexPath.row;
return cell;
}
-(void)fav:(id)sender
{
UIButton * btn = (UIButton *)sender;
btn.selected = !btn.selected;
int index = [[btn superview] tag]; // use this index for storing selections
}
Then make a array for storing selections. Compare this selection in cellForRowAtIndexPath and make btn selected or not selected.
Hope this will help you.....
You need to understand the way the cell is being reused. When your cell is scrolled out of the screen, it will be reused for the cell that is entering the screen. Table view will keep calling the method cellForRowAtIndexPath every time they want to render the cell. In your code, you init a new favbtn all the time, so even if the cell already contains it and being reused, you will keep on adding more buttons into it. Here's my suggestions:
Create your own subclass of UITableViewCell. Let's say FavoriteCell. Put all your code to add the favorite button inside your subclass.
In ViewDidLoad register your cell with your table view. Something like this: [self.tableView registerClass:...] or [self.tableView registerNib:...] depending on if you have a nib file for your cell or not.
In cellForRowAtIndexPath, dequeue your cell as usual but you don't have to check if it's nil or not, and you don't have to create the favorite button since you already put it inside your subclass.
Just remember that, the method cellForRowAtIndexPath will be called all the time so your logic of inserting favorite button has to be done properly. Make sure that you only insert it once and update its property properly.

How to select radio button at a time in table view

I am having a radio button in table view cell. This is my radio button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex])
{
newRadioButton.selected = YES;
}
else
{
newRadioButton.selected = NO;
}
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[table reloadData];
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
{
[sender setSelected:YES];
}
}
Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).
I need to make it look like the first image.
I think you should try:
in YourViewController.h
#interface YourViewController : UITableViewController {
NSIndexPath *selectedIndex
}
in YourViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.seleted = YES;
} else {
newRadioButton.seleted = NO;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
this link may help you
Unselect all the radio buttons before your if statement using something like this:
for (UITableViewCell *cell in [self visibleCells]) {
[[cell accessoryView] setSelected:NO];
}
When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property
you need to set the tag for everybutton inside cellForRowAtindexpth, something like
// where i is just a static int variable for holding the count
myRadioButton.tag = i ;
i=i+1
now you when you tap based on tag, you can check which button is pressed and you can select that one
to know which button is pressed u can know like this
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
Try to reuse button
Solution: we can add & access controls by tag from view
if control found by tag we can use it.
if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}

How to Handle Favourite button clicks in custom Tableview cells iOS?

I am developing one app and i have one requirement that. I have to handle the Favourite button on custom cells like. i am creating the custom button with image on cell and setting unselected type Favourite image i am giving by default once user click on the Favourite button on cell i am changing the button image like selected favourite. I am using the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Button Action:
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
NSLog(#"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
button.selected=!button.selected;
}
Using the above code i am able to change the change the Favourite button from normal to selected and selected to normal but problem is when i select the favourite button on first row it is changing 6 and 11 Ect.. rows also.
Can anybody suggest me right way to do this
Thanks in advance.
That button action and all things are looks fine. You need to save selected Button index as a tag in to the NSMutableArray like following example:
In.h Class:
interface myclass : UIViewController{
}
#property (strong, nonatomic) NSMutableArray *arrcontainstate;
In.m Class:
- (void)viewDidLoad {
[super viewDidLoad];
_arrcontainstate=[NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.row;
if ( [_arrcontainstate containsObject:indexPath.row]) {
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
else {
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
button.selected=!button.selected;
NSLog(#"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[_arrcontainstate addObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:#"favourites-Selected.png"] forState:UIControlStateNormal];
}
else
{
[_arrcontainstate removeObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
}
}
As you are reusing the cell, the image of the button is not changing. First cell is reused in 6th and 11th cell so the button image remains selected. Use this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton setBackgroundImage:[UIImage imageNamed:#"favourites-normal.png"] forState:UIControlStateNormal];
[cell.favButton addTarget:self action:#selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

UiButton text wont stay changed in Tableview cell

I have searched all over the web and cant seem to find a solution to this simple problem. I have a table view of buttons when the work "like". When the Button is pressed, it changes the word to "Unlike". I got it to work but when I scroll down the table, I see other buttosn also change to "unlike" and sometimes overlaps it with "Like". And when I scroll back up, the original button I selected changes back to normal state. I understand the cells are reusable and thats why I am using a mutable array for my data source and still it doesnt work. Please help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setTitle:#"Like" forState:UIControlStateNormal];
myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
myButton.tag =indexPath.row;
[cell.contentView addSubview:myButton];
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
the action method:
-(void)tapped:(id)sender {
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *parentCell = [[sender superview]superview];
NSIndexPath *indexPathLiked = [table indexPathForCell:parentCell];
[array replaceObjectAtIndex:senderButton.tag withObject:[NSNumber numberWithInt:1]];
[sender setTitle:#"Unlike" forState:UIControlStateNormal];
}
If the table calls the cellForRowAtIndexPath you create all the time a new button. When you get a reused cell the button still exists and you put a new one over there.
Change your method to:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setTitle:#"Like" forState:UIControlStateNormal];
myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
myButton.tag =indexPath.row;
[cell.contentView addSubview:myButton];
}
else {
// todo: change the button title to "like" or "unliked" value
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
P.S. This make no sense, you doesn't use that, why you do that?
UITableViewCell *parentCell = [[sender superview]superview];
NSIndexPath *indexPathLiked = [table indexPathForCell:parentCell];
If you have only one section you can get the cell without using superview:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]
UITableViewCell *parentCell = [table cellForRowAtIndexPath:indexPath];

Add permanent checkmark to tableView-cell

my viewController contains a tableView where the user can add cells via coreData.
I defined the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Travel";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
UILabel *countryLabel = (UILabel *)[cell viewWithTag:101];
kategorieLabel.text = [travel valueForKey:#"country"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
if ([[travel valueForKey:#"tick"] isEqualToString:#"yes"]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
- (void)buttonTapped:(UIButton *)sender {
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
[owningCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSIndexPath *indexPath = [_tableView indexPathForCell:owningCell];
NSManagedObject *travel = [self.travelling objectAtIndex:indexPath.row];
[travel setValue:#"yes" forKey:#"tick"];
}
When the user taps a button on the cell, the checkmark should appear. But when I reload or restart the app not all checkmarks are there. Maybe there is a better method to do this.

Resources