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

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.

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.

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

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.

Set two buttons on each cell with different action but same frame

I need to set two buttons on each cell with different actions but same frame, on click one will hide other will visible. How can i do this.
Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell =
[tableHistory dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
else
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"check"])
[button setImage:[UIImage imageNamed:#"right-with-bg.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"tick-bg.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arrayTktcode objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:btnUnScan];
[cell addSubview:btnUScan];
return cell;
}
-(void)buttonClicked:(id)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableHistory];
NSIndexPath *indexPath = [tableHistory indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
[button setImage:[UIImage imageNamed:#"right-with-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"check"];
}
else
{
[button setImage:[UIImage imageNamed:#"tick-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
}
}

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;
}

UITableViewCell and images overlapping

i have a strange issue with my UITableView...I add a UIButton as a subview to each cell , but when any of the cells gets out of view something happens, and when i scroll up again , i can see that the UIButton background images of some cells are overlapping !
The cellForRowAtIndexPath method i use contains the following code , which adds the UIButton instances
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"MyCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UIFont *titleFont = [UIFont fontWithName:#"Arial-BoldMT" size:14.0];
[[cell textLabel] setFont:titleFont];
[cell autorelease];
}
if([cell viewWithTag:indexPath.row]==nil){
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
buttonLeft.tag=indexPath.row;
if ([[myArray objectAtIndex:indexPath.row] isEqualToString:#"item1"]) {
[buttonLeft addTarget:self action:#selector(doThat:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundImage:[UIImage imageNamed:#"item1.png"] forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 5.0f, 6.2f, 30.0f, 30.0f)];
}else{
[buttonLeft addTarget:self action:#selector(doThat:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundImage:[UIImage imageNamed:#"item2.png"] forState:UIControlStateNormal];
[buttonLeft setFrame: CGRectMake( 5.0f, 6.2f, 30.0f, 30.0f)];
}
[cell addSubview:buttonLeft];
}
cell.textLabel.text=[NSString stringWithFormat:#" %#",[myArray objectAtIndex:indexPath.row]];
return cell;
}
Apparently , this code for some reason adds the UIButton everytime the cell is displayed. I want each button subview to be added only once..
What am i doing wrong ?
Your help is much appreciated
That is because the table view reuses cells for performance reasons. Therefore, the cellForRowAtIndexPath: method, correctly implemented like yours, returns cells that already exist and only creates a new one if there is no reusable cell available.
Put all your button-related code within if (cell == nil) {}, so that the button is only added to "fresh" cells and it should work!