I am using radio button on UITableView cell. Firstly radio button unchecked. When radio button is check. I m Scrolling UITableView the radio button is unchecked. Using this code is below
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifierFirst = #"CellFirst";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
}
NSArray *cellSubs = cell.contentView.subviews;
for (int i = 0 ; i < [cellSubs count] ; i++) {
[[cellSubs objectAtIndex:i] removeFromSuperview];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *redioBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)];
[redioBtn1 setImage:[UIImage imageNamed:#"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn1 addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn1.tag = ++tagCount;
[cell.contentView addSubview:redioBtn1];
UIButton *redioBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)];
[redioBtn2 setImage:[UIImage imageNamed:#"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn2 addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn2.tag = ++tagCount;
[cell.contentView addSubview:redioBtn2];
UIButton *redioBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)];
[redioBtn3 setImage:[UIImage imageNamed:#"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn3 addTarget:self action:#selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn3.tag = ++tagCount;
[cell.contentView addSubview:redioBtn3];
return cell;
}
-(void)selectRadioButon:(id)sender {
btnTag = [sender tag];
NSArray *arr = self.view.subviews;
UITableView *tblCell = [arr objectAtIndex:0];
NSArray *cellAry = tblCell.subviews;
for (int i = 0; i <[cellAry count]; i++) {
UITableViewCell *content = [cellAry objectAtIndex:i];
NSArray *contentArry = content.contentView.subviews;
for (int j = 0; j <[contentArry count]; j++) {
UIButton *button = [contentArry objectAtIndex:j];
if (btnTag == button.tag) {
for (int i = 0; i <[contentArry count]; i++) {
UIButton *button = [contentArry objectAtIndex:i];
if (btnTag == button.tag) {
if ([sender currentImage]==[UIImage imageNamed:#"radioselect.png"])
[button setImage:[UIImage imageNamed:#"radiounselect.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"radioselect.png"] forState:UIControlStateNormal];
}
else
[button setImage:[UIImage imageNamed:#"radiounselect.png"] forState:UIControlStateNormal];
}
return;
}
}
}
}
First thing: You should not be adding the UIButton's everytime the cell is loading. You should add them inside if (cell == nil) block.
Next thing: You should keep track of the selected radio button index (an int value) in a sepearate array (a NSMutableArray), and select the respective UIButton, outside the if (cell == nil) block.
I hope this tutorial will help you.
Related
I am just implementing a table view in my app. I have done this multiple times but this time the table cell shows the repeating data and speed of table scrolling is not good also. Just posting my code for table. Any help to prevent cell data repetition and scroll speed
optimisation will be appreciated.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [array count];
if (count%3 == 0)
{
return count;
}
else
{
count = count/3+1;
}
NSLog(#"Row count is%i",count);
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
int x = 15;
int y = 15;
int p = 10;
int q = 10;
for (int i = 0; i < 3; i++)
{
if ( indexPath.row*3+i < [array count] )
{
UIImageView *img = [[UIImageView alloc] init];
UIButton *btn = [[UIButton alloc] init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
placeholderImage:[UIImage imageNamed:#"India.png"]
success:^(UIImage *image) {}failure:^(NSError *error){}];
img.frame = CGRectMake(x, y, 140, 201);
btn.frame = CGRectMake(p, q, 150, 211);
btn.tag = indexPath.row*3+i;
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_N.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_S.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[testView addSubview:btn];
x = x+160;
p = p+160;
}
else
{
NSLog(#"No data");
break;
}
}
[cell addSubview:testView];
return cell;
}
The Things updated in cell is for the purpose to add 3 buttons in single cell. Nothing else.
SUGGESTION
Use custom cell
Fix :
Creating and adding the subview is to be included inside the braces where the cell is allocated
ie
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
//CREATING AND ADDING SUBVIEW SHOULD BE HERE
}
//Change in values is taken care here
REASON
Otherwise every time a subview is created,allocated all objects and added above when table is scrolled
REFER
Apple Docs
The problem you are facing is that you add a UIView to every cell that comes into the screen. However you are reusing the cells so the old 'added' views are still on them. The repetition of the data is because there are multiple layers of testviews on a cell when you scroll.
You should either make a CustomCell that includes the testview that you are adding at every cellforrow: OR add the testview to the cellviews when the cell is still a nil-value so that if it gets reused you won't add yet another testview to the cell and they overlap. The examples of those are written in other answers here but are found on the web everywhere.
Not recommended but:
Another quick but dirty fix is to just remove all subviews from the cell (or any subview that you add the testview to) for(UIView *v in [cell(.contentView) subviews]){ [v removeFromSuperView]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
int x = 15;
int y = 15;
int p = 10;
int q = 10;
for (int i = 0; i < 3; i++)
{
if ( indexPath.row*3+i < [array count] )
{
UIImageView *img = [[UIImageView alloc] init];
UIButton *btn = [[UIButton alloc] init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
placeholderImage:[UIImage imageNamed:#"India.png"]
success:^(UIImage *image) {}failure:^(NSError *error){}];
img.frame = CGRectMake(x, y, 140, 201);
btn.frame = CGRectMake(p, q, 150, 211);
btn.tag = indexPath.row*3+i;
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_N.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_S.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[testView addSubview:btn];
x = x+160;
p = p+160;
}
else
{
NSLog(#"No data");
break;
}
}
[cell addSubview:testView];
}
return cell;
}
You should not initialize anything outside the block where your cell is initialized, because when you scroll your tableview the method cellForRowAtIndexPath get called everytime, and your view testview is getting created everytime..
Outside the cell initialization you should only update the values to be shown in the cell..
Hope above works..
Edited
But better approach I would suggest try to create a Custom Cell so that you can easily access the components added to your cell to update their values as your tableview scrolls..
Thanks.
Repeating data in different cells might be due to same "CellIdentifier", try having different "CellIdentifier" for different cell view types. I can see different cell view types since you are having some condition block in your code.
static NSString *MyIdentifier;
MyIdentifier = #"Type1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
MyIdentifier = #"Type2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
hello this problem is normal when you are using dequeueReusableCell
just make change as i listed below in following methode..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
UIView *testView;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
testView.tag = 111;
[cell addSubview:testView];
[testView release];//For Non ARC
}
else
{
testView = [cell.contentView viewWithTag:111];
for(UIView * vv in [testView subViews])
{
[vv removeFromSuperView];
}
}
int x = 15;
int y = 15;
int p = 10;
int q = 10;
for (int i = 0; i < 3; i++)
{
if ( indexPath.row*3+i < [array count] )
{
UIImageView *img = [[UIImageView alloc] init];
UIButton *btn = [[UIButton alloc] init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
placeholderImage:[UIImage imageNamed:#"India.png"]
success:^(UIImage *image) {}failure:^(NSError *error){}];
img.frame = CGRectMake(x, y, 140, 201);
btn.frame = CGRectMake(p, q, 150, 211);
btn.tag = indexPath.row*3+i;
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_N.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"Picframe_S.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[testView addSubview:btn];
x = x+160;
p = p+160;
}
else
{
NSLog(#"No data");
break;
}
}
return cell;
}
TRY THIS....
I am trying to get a number from an array and store it in a button in UITableview cell. When I tap the button, I should be able to get the number. Whats the right way to get the information from the sender in the button click event
This is what I tried.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil )
{
UIButton *button = [[[UIButton alloc]init]autorelease];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:#"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(onIconTapped) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
[button setHidden:YES];
cell.accessoryView = button;
}
int myContactID = contact.contactID;
NSLog(#"My No. %d", myContactID);
NSNumber *mySelectedNumber = [NSNumber numberWithInt:myContactID];
UIButton *myButton = (UIButton *)[cell.accessoryView viewWithTag:1001];
for(int i = 0; i< [myContactsArray count]; i++){
if([[[myContactsArray objectAtIndex:i]valueForKey:#"RecordID"] isEqualToNumber:mySelectedNumber]){
[myButton setHidden:NO];
NSString *myString;
myString = [[myContactsArray objectAtIndex:i]valueForKey:#"MYNumber"];
[myButton setTitle:myString forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont systemFontOfSize:0];
NSLog(#"my number %#", myString);
NSLog(#"Button tag %#", myButton.currentTitle);
break;
}
else{
[myButton setHidden:YES];
}
}
}
-(void)onIconTapped:(id)sender{
}
-(void)onIconTapped:(id)sender{
if (sender.tag == 1001){
NSString *title = sender.currentTitle;
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *titleNumber = [numberFormatter numberFromString:title];
}
}
This approach is from iOS Recipes (Matt Drance): You can get the cell with converting the origin of the button to the tableView (convertPoint:toView:) and then call
[[self tableView] indexPathForRowAtPoint: convertedPoint];
1.
[button addTarget:self action:#selector(onIconTapped:) `enter code here`forControlEvents:UIControlEventTouchUpInside];
2.
myString = [[myContactsArray objectAtIndex:i]valueForKey:#"MYNumber"];
button.tag = i;`
3.
-(void)onIconTapped:(id)sender{
Contact *contact = [myContactsArray objectAtIndex:sender.tag];
}
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;
}
}
Hi I try this code but it not show image on cell where i am wrong i want when i click every cell then i want to display image or when i scroll cell then image not disappear it should be appear on that cell i use this code but it not working pease help me on this.
#interface imageclass : UITableViewController
{
BOOL changeimagetype;
//UIImage *btnImage;
UIButton *mimageButton;
UIImageView *onButtonView;
}
#property (nonatomic,retain)UIImageView *onButtonView;
#property BOOL changeimagetype;
#property (nonatomic,retain)UIButton *mimageButton;
-(void)changeMapType:(id)sender;
#end
#import "imageclass.h"
#implementation imageclass
#synthesize onButtonView,changeimagetype,mimageButton;
-(void)changeMapType:(id)sender
{
changeimagetype =!changeimagetype;
if(changeimagetype == YES)
{
//[typechange setImage:[UIImage imageNamed:#"map.png"] forState:UIControlStateNormal];
//self.myGreatMapView.mapType = MKMapTypeStandard;
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
//someBarButtonItem.image = [UIImage imageNamed:#"alarm_ON..png"];
//changeimagetype =NO;
}
else
{
//self.myGreatMapView.mapType = MKMapTypeSatellite;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
}
- (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];
}
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
// Configure the cell...
return cell;
}
Define you tableview delegate like this.
- (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];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
}
// Configure the cell...
return cell;
}
Here is the code:-
Add this in cellForRowAtIndexPath method. And also set flag variable in your target method for togglebutton.
if(flagButton)
{
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
}
else
{
onButtonView.image = [UIImage imageNamed:#"alarm_OFF.png"];
}
I tried this different approach, and it worked.
While configuring the button,
- (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];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
[tempButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
}
// Configure the cell...
return cell;
}
And in changeMapType: method,
-(void)changeMapType:(id)sender{
changeimagetype =!changeimagetype;
sender.selected = changeimagetype;
}
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.