iOS - Unable to call action from dynamically created button in UITableViewCell - iphone

i have created two button programmatically in UITableView i.e Edit Delete
when we click the cell these buttons are displayed but when i try to click in edit or delete button it doesn't calls the appropriate method i.e edit or deleteBtn.
This is my code.
-(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]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[[UIButton alloc]init];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
return cell;
}
my edit and delete function are very simple
-(void)edit{
NSLog("%#",selectedValue);
}
-(void)deleteBtn{
NSLog("%#",selectedValue);
}
i have checked this function applying breakpoint but it is not called.
this is how my selectedValue comes
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedValue=[firstName objectAtInder:indexPath.row];
}
Thanks, in advance,
Arun.

Think,the problem is with UIButton frame's.
in your code both button's origin.y is 100.0,so it went beyond the cell's bound (Default height is 44.0).
Change your Both UIButton frame like this,and it is worked for me.
UIButton * edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
UIButton* delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];

please your code like below
-(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]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:#selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];
delete=[[UIButton alloc]init];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:#selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];
return cell;
}
use methods like below
-(IBAction)edit:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%#",str);
}
-(IBAction)deleteBtn:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%#",str);
}

Your buttons are being added over and over to the cell because they're being created every time you call CFRAIP. Move them into the cell == nil block:
-(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]autorelease];
edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
return cell;
}

The problem is in the setFrame
The height of the table cell is less than the co-ordinates you have set for the buttons.
Change the code to
[edit setFrame:CGRectMake(100, 5, 100, 20)];
and this will work.
OR
just increase the height of the table rows, which by default is 44, using
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Please try to use this one ....and do same thing for delete button ..........
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// -------------------- Add button to cell --------------
edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:edit];
}

I can you try to custom cell and can you add buttons in custom cell view and implement below codes.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//****cell recent deal buy button click goes to login or check out pageview
//[cell.btn setTag:indexPath.row];
//[cell.btn addTarget:self action:#selector(buybutton_checkout:)
// forControlEvents:UIControlEventTouchDown];
// cell.btn.hidden=NO;
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"icel" owner:nil options:nil];
for (UIView *view in views)
{
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (icel*)view;
}
}
}
//tbl.layer.cornerRadius = 3.9;
//[tbl setClipsToBounds:YES];
[cell.activity startAnimating];
[cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
[cell.btn setTag:indexPath.row];
[cell.btn addTarget:self action:#selector(Buy_btnlck:)
forControlEvents:UIControlEventTouchDown];
//cell.btn.hidden=NO;
cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];
index_tbl=indexPath.row;
return cell;
}
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:#"%ld",(long int)[button tag]]intValue];
}

Don't use [UIButton alloc] init]. Use instead this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(edit:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Edit" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];
Where view is the view where you are adding your button as a subview. you can also set Button frame according to your requirements.

Pls change the button names , as Edit and delete are already defined keywords in Objective C

Modify your code as like this....
Replace the below two lines...
edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];
as like this...
UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];

Try this::
Table Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell.
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
[btnEdit setTitle:#"Edit" forState:UIControlStateNormal];
[btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
[btnEdit setTag:indexPath.row];
[btnEdit addTarget:self action:#selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnEdit];
UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
[btnDelete setTitle:#"Delete" forState:UIControlStateNormal];
[btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
[btnDelete setTag:indexPath.row];
[btnDelete addTarget:self action:#selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnDelete];
}
return cell;
}
Then, Button Action Methods
-(IBAction)clickEdit:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %#",str);
}
-(IBAction)clickDelete:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %#",str);
}
Hopefully, it'll help you.
Thanks.

Related

Display button click count in label for particular cell which are subviews to uitableview

I have a button and a label which are sub views to UITableView.
Initially label value is 0.
what i need is, when i click button on particular cell i want to increment value in same cell label (as 1) and display that value in same label.
And again i clicked same cell button the label in that cell should be increment (as 2) and display that value in same cell in UITableView.
My code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Lbl;
UIButton *btn;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Lbl =[[UILabel alloc]init];
[Lbl setFrame:CGRectMake(56, 60, 117, 12)];
[Lbl setBackgroundColor:[UIColor clearColor]];
[Lbl setTextAlignment:NSTextAlignmentLeft];
[Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
Lbl.tag=indexPath.row;
[cell.contentView addSubview:Lbl];
btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"add" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
[[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
[btn setFrame:CGRectMake(289, 2, 30, 71)];
btn.tintColor=[UIColor lightGrayColor];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
} else {
Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
btn =(UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
cell.textLabel.text=#"title";
countLbl.text = [[NSString alloc] initWithFormat:#"%d",showItemCount];
return cell;
}
// button action method
-(void)increaseItemCount:(UIButton *)sender
{
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(#"row: %d",path.row);
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:path.row];
showItemCount=[countLbl.text intValue] + 1;
NSLog(#"%d",showItemCount);
countLbl.text = [[NSString alloc] initWithFormat:#"%d",showItemCount];
}
I tried this, After clicking the value is showing in other cells and when i scroll the table view that value is showing in all cells.
Any suggestions or code
If you want source code, Download it from here https://github.com/MasudShuvo/TestCustomTableViewCell
I've modify the code as your requirement.
That's because it looks like you're using a single instance variable to store the itemCount. You should use an array to know which cell has been clicked how many times.
The code is however pretty messy, so you should write it again from scratch
indexPath.row begins 0 to n, we not set tag 0 for label and you use single variable showItemCount used to assign text in countLbl, table view reuse cell when it will appear,use array to store showItemCount for each cells
please do as per following:
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selectedIndex=-1;
//[tblView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UILabel *Lbl;
// UIButton *btn;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *Lbl =[[UILabel alloc]init];
[Lbl setFrame:CGRectMake(56, 60, 117, 12)];
[Lbl setBackgroundColor:[UIColor clearColor]];
[Lbl setTextAlignment:NSTextAlignmentLeft];
[Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
Lbl.tag=indexPath.row;
Lbl.text=[NSString stringWithFormat:#"%i",0];
[cell.contentView addSubview:Lbl];
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"add" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
[[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
[btn setFrame:CGRectMake(289, 2, 30, 71)];
btn.tintColor=[UIColor lightGrayColor];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
NSLog(#"Selected Index:%i",indexPath.row);
} else {
UILabel *Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
//UIButton *btn =(UIButton *)[cell viewWithTag:indexPath.row];
if(indexPath.row==selectedIndex)
{
NSLog(#"Selected Index in CellForRowAtIndexPath:%i",indexPath.row);
Lbl.text=[NSString stringWithFormat:#"%i",[Lbl.text intValue]+1];
}
}
// cell.textLabel.text=#"title";
//countLbl.text = [[NSString alloc] initWithFormat:#"%d",showItemCount];
return cell;
}
-(void)increaseItemCount:(UIButton *)sender
{
selectedIndex=sender.tag;
[tblView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected Index:%i",indexPath.row);
}
i hope this will help you.

Stop reloading of table data in UITableView when it pulls/scrolls

I know it is not general practice to do this but my application need this functionality. I am very new to iPhone programming. When the user pulls/scrolls the table screen it gets updated I have to stop it. Also order of the data get changed when I pull it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[button addTarget:self action:#selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:optionValue forState:UIControlStateNormal];
[button setTag:indexPath.row];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button release];
}
return cell;
}
First, the way you are using cellForRowAtIndexPath is wrong.
static NSString *MyIdentifier = #"MyIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[button addTarget:self action:#selector(optionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button release];
}
//These should be outside of the cell == nil loop so that they get refreshed correctly
[button setTitle:optionValue forState:UIControlStateNormal];
[button setTag:indexPath.row];
return cell;
Secondly, I don't see where you set optionValue, but if you don't want to hit the database every time, then store the values in an NSArray and get the values from the array. This way you only hit the database once when you load the view.

Add/remove image on button press

This my tableview.i want when user press button then another image should on the top of that image and removed when again user presses wt is the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect prescriptionFrame=CGRectMake(100, 0, 150, 40);
UILabel *presTextLabel=[[UILabel alloc] initWithFrame:prescriptionFrame];
presTextLabel.font=[UIFont boldSystemFontOfSize:20];
CGRect tabletFrame=CGRectMake(150, 50, 100, 20);
UILabel *tabletTextLabel=[[UILabel alloc] initWithFrame:tabletFrame];
tabletTextLabel.font=[UIFont boldSystemFontOfSize:10];
CGRect noOfTabletFrame=CGRectMake(250, 40, 30, 30);
UILabel *noOfTabletTextLabel=[[UILabel alloc] initWithFrame:noOfTabletFrame];
noOfTabletTextLabel.font=[UIFont boldSystemFontOfSize:10];
cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.frame=CGRectMake(10,10 ,50,50);
presTextLabel.text=[appDelegate.prescriptionArray objectAtIndex:indexPath.row];
tabletTextLabel.text=[appDelegate.tabletArray objectAtIndex:indexPath.row];
noOfTabletTextLabel.text=[appDelegate.noOfTabletsArray objectAtIndex:indexPath.row];
[cellButton setImage:[appDelegate.imageArray objectAtIndex:indexPath.row]
forState:UIControlStateNormal];
[cellButton addTarget:self action:#selector(StartTimer)
forControlEvents:UIControlEventTouchUpInside];
cellButton.tag = indexPath.row;
[cell.contentView addSubview:presTextLabel];
[cell.contentView addSubview:tabletTextLabel];
[cell.contentView addSubview:noOfTabletTextLabel];
[cell.contentView addSubview:cellButton];
[presTextLabel release];
[tabletTextLabel release];
return cell;
}
In
- (void)StartTimer:(id)sender;
do
UIButton* button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell*)[[button superview] superview];
NSIndexPath indexPath = [self.tableview indexPathForCell:cell];
if([button imageForState:UIControlStateNormal] == [appDelegate.imageArray objectAtIndex:indexPath.row]) {
[button setImage:[UIImage imageNamed:#"NEWIMAGE.png"] forState:UIControlStateNormal];
}
else {
[button setImage:[appDelegate.imageArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
}
Not tested, may include typos, but should work.
MfG,
SideSwipe

how can I take index of particular row by clicking a button(targeting to method)?

I have made a table and contents some values, I have a button coding like
cell.textLabel.textColor = [UIColor whiteColor];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:#"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:#selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
I want to click that button and take values from that particular index,
like
Ali (button here) --------index 0
Sajid (button here) --------index 1
Robert (button here) --------index 2
RaM (button here) --------index 3
Theser are cells of table, now how can I get index as I click that button?
if you are not getting my question, you can ask me again...
I found my answer,
Just write the following coding inside the button targeted method and use clickButtonPath as your index path.
It is working perfectly, simply
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
btn.tag = indexPath.row;
Then in (void)editTable:(id)sender:
int row = (UIButton *)btn.tag;
you would use something like this.
- (IBAction)editTable:(UIButton *)sender {
UIView *contentView = [sender superView];
UITableViewCell *cell = [contentView superView];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
But first you should make sure to properly reuse your cells. By changing your code like this:
cell = ... dequeue cell ...
if (cell == nil) {
cell = ... create new cell ...
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = 194;
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageNamed:#"edit_button.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(290, 15, 15, 15)];
[btn addTarget:self action:#selector(editTable:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
}
// if you need to change something for this button you can get it like this:
//UIButton *btn= [cell.contentView viewWithTag:194];
NSString *cellValue = [myArrayNew objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;

Checkboxes in UITableView

I am trying to create a list of options with check boxes. I am using UITableView and adding custom type buttons to the table as checkboxes.
My code for cellForRowAtIndexPath method is pasted below:
- (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] autorelease];
}
CGRect a=CGRectMake(0, 0, 310, 32);
UIImageView *aImg=[[UIImageView alloc] initWithFrame:a];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
aImg.image=[[[Resources getResources] getImageLoader] getMainMenuUnselectedImage];
bImg.image=[[[Resources getResources] getImageLoader] getLiveScoreSmallBg];
[aImg setContentMode:UIViewContentModeScaleToFill];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.backgroundView = [[UIView alloc] initWithFrame:a];
[cell.backgroundView addSubview:aImg];
cell.selectedBackgroundView=bImg;
cell.font = [UIFont boldSystemFontOfSize:16];
cell.selectedTextColor = [UIColor blackColor];
UIImage *checked = [UIImage imageNamed:#"check_filled.png"];
UIImage *unchecked = [UIImage imageNamed:#"check_empty.png"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(3, 3, 30, 30)];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button setImage: [UIImage imageNamed:#"check_filled.png"] forState: UIControlStateSelected];
[button setImage: [UIImage imageNamed:#"check_empty.png"] forState: UIControlStateNormal];
[button setEnabled:YES];
button.tag = indexPath.row;
[button setUserInteractionEnabled:YES];
[cell.contentView addSubview:button];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 3, 250, 17)];
label.font = [UIFont boldSystemFontOfSize:15];
label.backgroundColor = [UIColor clearColor];
label.text = [teamsList objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];
[button release];
[label release];
[aImg release];
[bImg release];
return cell;
}
The problem is that when I run the program, the screen appears without buttons. Also, when I click any row, the program crashes.
There're some problems with your code which may be not relevant to your problem, but still...
You create cell contents each time cell is reused - so if you scroll your table back and forth the reused cells will contain multiple identical subviews - that's not what you want. To fixed that you must move creating contents to the creating cell block:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Create cell contents here!
...
}
...
You must not release your button - since you create it with convenience method button is autoreleased
button = [[UIButton buttonWithType:UIButtonTypeCustom]
initWithFrame:CGRectMake(3, 3, 30, 30)];
This line may also cause problems:
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
Here you set selector that receives no parameters. If your buttonPressed method declared is (most common way)
- (void) buttonPressed:(id)sender;
Then you must create selector as #selector(buttonPressed:) to indicate that it actually takes 1 parameter
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
int i=indexPath.row;
ibutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ibutton setTitle:#"i" forState:UIControlStateNormal];
ibutton.frame = CGRectMake(430.0,40.0,100.0,60.0);
[cell.contentView addSubview:ibutton];
[ibutton setTag:indexPath.row];
NSLog(#"the button tag is %d",ibutton.tag);
[ibutton addTarget:self action:#selector(detailSubView:) forControlEvents:UIControlEventTouchUpInside];
addbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[addbutton setTitle:#"add" forState:UIControlStateNormal];
addbutton.frame=CGRectMake(560, 40, 100, 60);
[addbutton setTag:indexPath.row];
[cell.contentView addSubview:addbutton];
[addbutton addTarget:self action:#selector(addclicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(350.0, 60.0, 50.0, 25.0)];
[label setBackgroundColor:[UIColor whiteColor]];
label.text=#"2.99$";
label.font = [UIFont boldSystemFontOfSize:18];
[cell.contentView addSubview:label];
[label release];
[cell.contentView addSubview:lab];
return cell;
}
-(IBAction)detailSubView:(id)sender{
NSLog(#"working in detail view");
UIButton *button = (UIButton *)sender;
NSLog(#"%d",button.tag);
a=button.tag;
NSLog(#"%d",a);
category1* B2 = [[category1 alloc] init];
UINavigationController *B2Nav = [[UINavigationController alloc] initWithRootViewController:B2];
B2Nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:B2Nav animated:YES];
[B2 release];
[B2Nav release];
}
-(IBAction)addclicked:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d",button.tag);
a=button.tag;
}