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
Related
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.
I have created a custom cell like
In this "+" sign increases the ticket count and "-" decreases it. When ticket count is 0, "-" is disabled and when ticket count is 4 "+" is disabled. It is working fine till now. The problem is Now i want to change the frame size of table when I click the "+" button of 4th row and regain the Frame size when I click "-" of 4th row. When i do so "-" functionality of other row changes randomly.PLease let me know what am i doing wrong.Thanks 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 = [[UI
Label 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];
minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
minusbutton.tag=104;
[minusbutton setEnabled:NO];
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];
addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
addbutton.tag=105;
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(240, 10, 80, 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];
NSString *string2=[[NSString alloc]initWithFormat:#"%#",[[eventTicketListArray objectAtIndex:indexPath.row ]price] ];
labelTwo.text =string2;
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",[[eventTicketListArray objectAtIndex:indexPath.row ]qty ] ];
}
return cell;
}
Method for + button click
-(void)IncreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str++;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:#"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
if (str==4) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
[btn setEnabled:YES];
}
}
if (row==3) {
ticketTable.frame=CGRectMake(0, 0, 320, 70);
}
[currentCell setNeedsDisplay];
}
Method for - button click
-(void)DecreaseTickets:(id)sender{
NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;
for(UILabel *lbl in [currentCell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
{
str = [lbl.text intValue];
str--;
eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
eventTicketList.qty=str;
lbl.text=[NSString stringWithFormat:#"%d",str];
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
{
if (str<=0) {
[btn setEnabled:NO];
}
}
}
for(UIButton *btn in [currentCell.contentView subviews])
{
if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
{
[btn setEnabled:YES];
}
}
if (row==3){
ticketTable.frame=CGRectMake(0, 0, 320, 200);
}
[currentCell setNeedsDisplay];
}
Add below Code in you button click or you can also use this method in didSelectRowForIndexPath.
[self.tableView beginUpdates];
[self.tableView endUpdates];
Use this will automatically handle tableview call Below Method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300;// here you can calculate cell height according to your requirements.
}
Hope, this will help you...
I have a tableview where I am displaying my values from database in tableview cells. In this tableview cellforrowatindexpath, I have created a button with an image on it and set selector for it. So when I click on the button my selector is called and it changes my image to another image.
But the problem is it does not identifies the indexpath i.e. if 4 rows are present in the tableview and if I click on the first row button, its image should change but problem is 4th row action is performed and its image is changed because it does not get the proper indexpath of the image to change.
This is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType::) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
This is my changemapType code
-(void)changeMapType:(NSIndexPath*)path1:(UIButton*)sender{
appDelegate.changeimagetype =!appDelegate.changeimagetype;
if(appDelegate.changeimagetype == YES)
{
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = YES;
}
else
{
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = NO;
}
}
Don't break table view cell reuse just to put a different tag on each button. If your cells are the same, use the same reuse identifier.
You can find out the index path of the sender like this, without any need to mess around with tags. It also works for multi section tables.
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
I don't think you can have additional arguments in an #selector like that. What you might have to do is subclass UIButton and add an NSIndexPath property (or even just an int) and in -(void)changeMapType:(MyCustomButton*)sender access the property there.
Also it seems you do not even use the index path in changeMapType so that will need to be changed as well.
EDIT: Is it the button image you are trying to change? In which case you don't need the index path at all, or a subclass. Just use [sender setImage:(UIImage *)image forState:(UIControlState)state].
Use yourTableView for getting the Index Path.. Try something like this.
- (void)buttonAction:(UIButton*)sender {
UITableViewCell *cell = (UITableViewCell*)button.superview;
NSIndexPath *indexPath = [yourTableView indexPathForCell:cell];
NSLog(#"%d",indexPath.row)
}
I think following code might help you..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *myCheckbox = [[UIView alloc] init];
UIButton *myBt1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
if([appDelegate.changeimagetype == YES"]){
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}
else {
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
[myBt1 addTarget:self action:#selector(btcheckbox:) forControlEvents:UIControlEventTouchDown];
[myBt1 setTag:indexPath.row];
[myCheckbox addSubview:myBt1];
[myBt1 release];
[myCheckbox release];
[cell addsubview:myview];
return cell;
}
-(void) btcheckbox:(id) sender
{
UIButton *currentButton = (UIButton *) sender;
if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_OF.png"]])
{
appDelegate.changeimagetype = YES;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}else if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_ON.png"]])
{
appDelegate.changeimagetype = NO;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = indexPath.row;
//onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
//onButtonView.tag = 2;
// onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
mimageButton.selected = NO;
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)changeMapType:(UIButton*)sender{
if(sender.selected == YES)
{
// onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[sender setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
sender.selected = NO;
}
else
{
//onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[sender setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
sender.selected = YES;
}
}
I have two buttons inside sectioned tableview cell thumbs up and thumbs down. Initially image of both button is not selected. When I select thumbs up button its image become thumbs up selected and other one become thumbsdown not selected and vice versa.
//Number of sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"mod:numberOfSectionsInTableView");
NSLog(#"[preferences count]=%d",[preferences count]);
return [preferences count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.choices count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
NSLog(#"cellForRowAtIndexPath: DISPLAY TEST");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"Text is: %#", [choices objectAtIndex:indexpath.row]);
NSLog(#"CHOICE AT INDEX PATH IS: %#",[choices objectAtIndex:indexpath.row %[choices count]]);
cell.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
// Thumbs up button.
//UIButton *thumbsUp = [[UIButton alloc]init];
thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
[thumbsUp addTarget:self action:#selector(thumbUp_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsUp setTitle:#"" forState:UIControlStateNormal];
thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
[thumbsUp setBackgroundImage:[UIImage imageNamed:#"thumbsup_not_selected.png"]
forState:UIControlStateNormal];
//NSLog(#"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
[cell.contentView addSubview:thumbsUp];
// Thumbs down button
thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsDown addTarget:self action:#selector(thumbDown_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsDown setTitle:#"" forState:UIControlStateNormal];
thumbsDown.frame = CGRectMake(200, 20, 20, 15);
[thumbsDown setTag:indexpath.row+120];
[cell.contentView addSubview:thumbsDown];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[thumbsDown setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]
forState:UIControlStateNormal];
}
NSLog(#"------------> TAG TEST %d",thumbsUp.tag);
cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];
NSLog(#"HELLO FOR TESTING");
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(15, 10, 300, 25);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentLeft;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
[view autorelease];
[view addSubview:label];
//[view addSubview:segmentedControl];
view.backgroundColor = [UIColor grayColor];
return view;
}
//Thumbs Up Button Action
- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
NSLog(#"Thumbs Up Check!");
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
NSLog(#"indexpath =%d",indexPath.row);
//[button setTag:indexPath.row+(indexPath.section * 50)];
int cTag = [sender tag];
NSLog(#"------>TAG : %d", cTag);
NSLog(#"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
if(cTag == (indexPath.row+(indexPath.section * 50)))
{
NSLog(#"BUTTON COUNT:");
[button setBackgroundImage:[UIImage imageNamed:#"thumbsup_selected.png"]
forState:UIControlStateNormal];
}enter code here
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
//int row = button.tag;
NSLog(#"SECTION IS:%d",section);
NSLog(#"ROW IS: %d",row);
NSArray *array = cell.contentView.subviews;
NSLog(#"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:2];
[test setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
}
Due to issue with tag of button while I change image of one button several buttons are changing.
If any one can please find a solution it will be helpful.... tag is setting for buttons in sections which we can view.
If the Tableview is not sectioned its working well.
Your problem might be the fact that you're setting the variable only if the cell is newly created. Perhaps the sectioning has an impact on how many cells are affected. Try pulling your thumbs up / thumbs down code out of the if (cell == nil) block and see if that has an effect.
I have two buttons inside sectioned tableview cell thumbs up and thumbs down.
Initially image of both button is not selected. When I select thumbs up button its image become thumbs up selected and other one become thumbsdown not selected and vice versa.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
enter code hereNSLog(#"mod:numberOfSectionsInTableView");
NSLog(#"[preferences count]=%d",[preferences count]);
return [preferences count];
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{enter code here
enter code herereturn [self.choices count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
NSLog(#"cellForRowAtIndexPath: DISPLAY TEST");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"Text is: %#", [choices objectAtIndex:indexpath.row]);
NSLog(#"CHOICE AT INDEX PATH IS: %#",[choices objectAtIndex:indexpath.row %[choices count]]);
cell.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
// Thumbs up button.
//UIButton *thumbsUp = [[UIButton alloc]init];
thumbsUp = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsUp setTag:(indexpath.row+(indexpath.section * 50))];
[thumbsUp addTarget:self
action:#selector(thumbUp_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsUp setTitle:#"" forState:UIControlStateNormal];
thumbsUp.frame = CGRectMake(150.0, 20, 20, 15);
[thumbsUp setBackgroundImage:[UIImage imageNamed:#"thumbsup_not_selected.png"]
forState:UIControlStateNormal];
//NSLog(#"------------------>TAG TEST : %d",(indexpath.row+(indexpath.section * 50)));
[cell.contentView addSubview:thumbsUp];
// Thumbs down button
thumbsDown = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbsDown addTarget:self
action:#selector(thumbDown_ButtonClicked:event:)
forControlEvents:UIControlEventTouchUpInside];
[thumbsDown setTitle:#"" forState:UIControlStateNormal];
thumbsDown.frame = CGRectMake(200, 20, 20, 15);
[thumbsDown setTag:indexpath.row+120];
[cell.contentView addSubview:thumbsDown];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[thumbsDown setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
}
NSLog(#"------------> TAG TEST %d",thumbsUp.tag);
cell.text = [choices objectAtIndex:(indexpath.row % [choices count])];
NSLog(#"HELLO FOR TESTING");
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(15, 10, 300, 25);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentLeft;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 0, 300, 60)];
[view autorelease];
[view addSubview:label];
//[view addSubview:segmentedControl];
view.backgroundColor = [UIColor grayColor];
return view;
}
//Thumbs Up Button Action
- (IBAction)thumbUp_ButtonClicked:(id)sender event:(id)event
{
NSLog(#"Thumbs Up Check!");
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
NSLog(#"indexpath =%d",indexPath.row);
//[button setTag:indexPath.row+(indexPath.section * 50)];
int cTag=[sender tag];
NSLog(#"------>TAG : %d", cTag);
NSLog(#"------> Calculated TAG %d",indexPath.row+(indexPath.section * 50));
if(cTag == (indexPath.row+(indexPath.section * 50)))
{
NSLog(#"BUTTON COUNT:");
[button setBackgroundImage:[UIImage imageNamed:#"thumbsup_selected.png"]
forState:UIControlStateNormal];
}
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
//int row = button.tag;
NSLog(#"SECTION IS:%d",section);
NSLog(#"ROW IS: %d",row);
NSArray *array = cell.contentView.subviews;
NSLog(#"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:2];
[test setBackgroundImage:[UIImage imageNamed:#"thumbsdown_not_selected.png"]forState:UIControlStateNormal];
}
Due to issue with tag of button while I change image of one button several buttons are changing. If any one can please find a solution it will be helpful....
This is a common occurrence due to the fact that the table view cells are recycled. Your scheme with the button tags is rather convoluted and error prone.
It would make more sense to keep track of the ThumbsUp/Down state of each cell in your table view's datatsource data model. Then, in cellForRowAtIndexPath: set each button state explicitly.
BTW: cell.text and cell.textColor are deprecated. You should use the properties of cell.textLabel.