iphone development: using tableview cells as a button - iphone

Is that possible to use tableView cells like a button? In my app I want to have a table view which has 4 rows and in each row there will be labels. What I want is when I touch a row, the table tableview cell should react like a button (perform an action and background is highlighted)? Is that possible? How can I do that? Thanks.

very simple the delegate method will be called didSelectRowAtIndexPath there you can get which row is tapped in which section and you can perform further action.
for example
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0){
LoginFormViewController *vController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
vController = [[LoginFormViewController alloc] initWithNibName:#"LoginFormViewController_ipad" bundle:nil];
}else {
vController = [[LoginFormViewController alloc] initWithNibName:#"LoginFormViewController" bundle:nil];
}
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}
}

You can implement this 'button' functionality in the tableView:didSelectRowAtIndexpath method in your UITableView delegate ;)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do something when pressed on an UITableViewCell
}

The cel will highlight, and your table view's delegate will receive a tableView:didSelectRowAtIndexPath: message anyway. You don't need anything extra to achieve what you have described.

- (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];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
}
if(tableView.tag==1)
{
cell.userInteractionEnabled = NO;
cell.backgroundColor =[UIColor blackColor];
UIButton *Btn11=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn11 addTarget:self action:#selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[Btn11 setBackgroundImage:[UIImage imageNamed:#"small_img4.png"] forState:UIControlStateNormal];
Btn11.tag=indexPath.row;
Btn11.frame=CGRectMake(0, 6.5, 156, 74.5);
UILabel *Main_tblImg_Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 80,156, 31)];
[Main_tblImg_Label setBackgroundColor:[UIColor blackColor]];
//[Main_tblImg_Label setText:#"First Drive 2012 Mercedes Benz M class"];
//Main_tblImg_Label.textAlignment = UITextAlignmentCenter;
[Main_tblImg_Label setTextColor:[UIColor whiteColor]];
[Main_tblImg_Label setFont:[UIFont fontWithName:#"Helvetica" size:9]];
[Main_tblImg_Label setNumberOfLines:2];
[cell.contentView addSubview:Main_tblImg_Label];
[cell.contentView addSubview:Btn11];
UILabel *other_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(14, 80, 142, 29)];
[other_Lbl setText:#"First Drive 2012 \nMercedes Benz M class"];
[other_Lbl setBackgroundColor:[UIColor blackColor]];
[other_Lbl setTextColor:[UIColor whiteColor]];
[other_Lbl setFont:[UIFont fontWithName:#"Helvetica" size:9]];
[other_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other_Lbl];
[cell.contentView addSubview:Btn11];
//[cell.contentView addSubview:Main_tblImg_Label];
// [cell.contentView addSubview:Btn11];
//
UIButton *Btn22=[UIButton buttonWithType:UIButtonTypeCustom];
[Btn22 addTarget:self action:#selector(ratingAction2:) forControlEvents:UIControlEventTouchUpInside];
[Btn22 setBackgroundImage:[UIImage imageNamed:#"Screen2.png"] forState:UIControlStateNormal];
Btn22.tag=indexPath.row;
Btn22.backgroundColor=[UIColor clearColor];
Btn22.frame=CGRectMake(165, 6.5, 156, 74.5);
UILabel *main_tblImg_Lbl2=[[UILabel alloc]initWithFrame:CGRectMake(165, 80, 156, 31)];
[main_tblImg_Lbl2 setBackgroundColor:[UIColor blackColor]];
//[main_tblImg_Lbl2 setText:#"Audi planning Mexican Assembly plant?"];
[main_tblImg_Lbl2 setTextColor:[UIColor whiteColor]];
//[main_tblImg_Lbl2 setFont:[UIFont fontWithName:#"times" size:1]];
[main_tblImg_Lbl2 setFont:[UIFont fontWithName:#"Helvetica" size:10]];
[main_tblImg_Lbl2 setNumberOfLines:2];
[cell.contentView addSubview:main_tblImg_Lbl2];
UILabel *other1_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(176, 80, 136, 31)];
[other1_Lbl setText:#"Audi planning Mexican Assembly plant?"];
[other1_Lbl setBackgroundColor:[UIColor blackColor]];
[other1_Lbl setTextColor:[UIColor whiteColor]];
[other1_Lbl setFont:[UIFont fontWithName:#"Helvetica" size:10]];
[other1_Lbl setNumberOfLines:2];
[cell.contentView addSubview:other1_Lbl];
[cell.contentView addSubview:Btn11];
[cell.contentView addSubview:Btn22];
}
return cell
}

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.

how to change label text in custom cell when button in that cell clicked

I have created a custom tableView with labels and buttons. On a button click in a cell i want to change the label text in that cell only. My problem is when i click button corresponding labels in other cell also changes. Here is my code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: CellIdentifier] autorelease];
UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100, 20)];
labelOne.tag = 100;
labelOne.backgroundColor=[UIColor clearColor];
labelOne.font=[UIFont systemFontOfSize:14];
[cell.contentView addSubview:labelOne];
[labelOne release];
UIButton *minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
minusbutton.backgroundColor=[UIColor clearColor];
[minusbutton addTarget:self action:#selector(DecreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[minusbutton setTitle:#"-" forState:UIControlStateNormal];
[minusbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[minusbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:minusbutton];
[minusbutton release];
UILabel *quantity = [[UILabel alloc]initWithFrame:CGRectMake(170, 5, 20, 20)];
quantity.tag = 103;
quantity.backgroundColor=[UIColor clearColor];
quantity.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:quantity];
[quantity release];
UIButton *addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
addbutton.backgroundColor=[UIColor clearColor];
[addbutton addTarget:self action:#selector(IncreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
[addbutton setTitle:#"+" forState:UIControlStateNormal];
[addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[addbutton setBackgroundImage:[UIImage imageNamed:#"button-green1.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:addbutton];
[addbutton release];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)];
labelTwo.tag = 101;
labelTwo.backgroundColor=[UIColor clearColor];
labelTwo.font=[UIFont systemFontOfSize:14];
labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1];
[cell.contentView addSubview:labelTwo];
[labelTwo release];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 230, 30)];
label3.tag = 102;
label3.backgroundColor=[UIColor clearColor];
label3.numberOfLines=2;
label3.font=[UIFont systemFontOfSize:12];
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
NSString *ss2=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]tit] ];
labelOne.text = ss2;
UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
labelTwo.text = [[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]price] ];
UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102];
label3.text=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]desc ] ];
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",qty];
}
return cell;
}
Initially qty=0
now i want that when i click addbutton, qty of that cell increases. here is my button click event
-(void)IncreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
//NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
UILabel *label4 = (UILabel *) [currentCell.contentView viewWithTag:103];
label4.text=[[[NSString alloc]initWithFormat:#"%d",qty++] ;
//NSLog(#"%d",row);
[ticketTable reloadData];
}
But the problem is when i click the button,qty of all cell increases. Please guide me how to do it. Any help will be appreciated.
Set condition like this..
set on global Variable NSUInteger myRow.
myRow = indexPath.row;//Set Selected index path.
in cellForRowAtIndexPath set condition.
if(myRow == indexPath.row)
{
NSUInteger newqty = qty + 1;
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",newqty];
}
esle
{
UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
label4.text=[[NSString alloc]initWithFormat:#"%d",qty];
}
and set some logic for that.
when you call
[ticketTable reloadData];
all your data reload and your table is initialized again
UITable magic is in reuse of a few allocated cells (just the minimus number of cell required: the viewable cells in a time on screen) and then you should just reuse them with new data setting when you scroll, typically using NSArray in your table data delegate...
and: where qty is defined? it seems it's a global variable, not one for every single cell...
you'd better to read some apple example code or some tutorial

How to insert a customized row below a row that is tapped

I am developing a project in which there is one requirement that in a table view when I tapped a row there should be one row added below that row and also I want that row to be customized as I want to add a text view in that row.
Please tell me how can I do this.
Any suggestions would be highly appreciated.
My current code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
UIImageView *imgViewBackground=(UIImageView *)[cell.contentView viewWithTag:20];
UIImageView *imgView=(UIImageView *)[cell.contentView viewWithTag:21];
UILabel *label1=(UILabel *)[cell.contentView viewWithTag:22];
UILabel *label2=(UILabel *)[cell.contentView viewWithTag:23];
UILabel *label3=(UILabel *)[cell.contentView viewWithTag:24];
[imgViewBackground setImage:[UIImage imageNamed:#"bba3.png"]
[imgView setImage:[UIImage imageNamed:#"picdefault.png"]];
[label1 setText:[self.usernameArray objectAtIndex:indexPath.row]];
[label2 setText:[NSString stringWithFormat:#"%#",[self.level1Array objectAtIndex:indexPath.row]]];
[label3 setText:[self.ageArray objectAtIndex:indexPath.row]]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if ([identifier isEqualToString:#"Cell"]) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 300, 98)];
imageView.tag=20;
[imageView setImage:[UIImage imageNamed:#"bba2.png"]];
[cell.contentView addSubview:imageView];
[imageView release];
UIImageView *imagePicView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 20, 60, 60)];
imagePicView.tag=21;
[cell.contentView addSubview:imagePicView];
[imagePicView release];
UILabel *namelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 20, 200, 20)];
namelabel.backgroundColor=[UIColor clearColor];
namelabel.font=[UIFont systemFontOfSize:15];
namelabel.tag=22;
[namelabel setTextAlignment:UITextAlignmentLeft];
[namelabel setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:namelabel];
[namelabel release];
UILabel *activitylabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 20)];
activitylabel.backgroundColor=[UIColor clearColor];
activitylabel.font=[UIFont italicSystemFontOfSize:14];
activitylabel.tag=23;
[activitylabel setTextAlignment:UITextAlignmentLeft];
[activitylabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:activitylabel];
[activitylabel release];
UILabel *agelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 60, 200, 20)];
agelabel.backgroundColor=[UIColor clearColor];
agelabel.font=[UIFont systemFontOfSize:13];
agelabel.tag=24;
[agelabel setTextAlignment:UITextAlignmentLeft];
[agelabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:agelabel];
[agelabel release];
}
return cell;
}
Look at the WWDC 2011 developer video for UITableView changes and tips - in there they describe how to do exactly that. Sadly there's not been any sample code I've ever been able to find, but they document the approach pretty well in the talk.

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

Selected state of UIButton in UITableView

I have UIButton in each cell of UITableView. When I touch it, its state is set to selected. But when I scroll table view so the button isn't visible, the state is set to normal. How can I do it that UIButton remain selected?
Thank you.
Edit: Here is the cellForRowAtIndexPath 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];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row < [messagesArray count]) {
Zprava *msgObj = [messagesArray objectAtIndex:indexPath.row];
int width = 0;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
width = 320;
} else {
width = 480;
}
CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);
CGSize requiredSize = [msgObj.mmessage sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
UIButton *background = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 300, requiredSize.height + 20)];
[background setBackgroundImage:[[UIImage imageNamed:#"balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal];
[background setBackgroundImage:[[UIImage imageNamed:#"selected-balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateSelected];
[background addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
background.tag = msgObj.msgID;
[[cell contentView] addSubview:background];
[background release];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 285, 15)];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setTextColor:[UIColor lightGrayColor]];
[dateLabel setNumberOfLines:0];
[dateLabel setTextAlignment:UITextAlignmentRight];
[dateLabel setText:msgObj.mdate];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setOpaque:NO];
[[cell contentView] addSubview:dateLabel];
[dateLabel release];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 275, requiredSize.height + 5)];
[messageLabel setFont:[UIFont systemFontOfSize:17]];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setNumberOfLines:0];
[messageLabel setText:msgObj.mmessage];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setOpaque:NO];
[[cell contentView] addSubview:messageLabel];
[messageLabel release];
}
return cell;
}
Save button states somewhere in your model separately from table view and set buttons state in cellForRowAtIndexpath: method again.