I have a tableview with each cell having eight buttons. I need horizontal scroll to be able to scroll those buttons.
While using this code, i can't see any buttons in cell.
In view did Load
horizontalScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 270, 320, 60)];
//horizontal scroll is a property of UIViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int x = 13;
int y=13;
for (int i = 0; i < 8; i++) {
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(x, 80, 45, 22);
[button setTag:i];
[button setTitle:#"08:30" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:234.0/255 green:234.0/255 blue:234.0/255 alpha:1]];
button.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:13.0];
[horizontalScroll addSubview:button];
x +=62;
}
[cell addSubview:horizontalScroll];
}
However , if i do :
[cell addSubview:button];
I can see buttons in cell but cannot scroll them.
Any help would be appreciated !!
Add [horizontalScroll setContentSize:CGSizeMake(height, WidthofyourContents)];
Scroller.contentSize =CGSizeMake([imgArray count]*75, 60);
otherwise,Please try to test with rounded rect buttons,its helpful for identify the problem.
If you want to add horizontal scrollview with in the each tableview cell ,
then you must add the horizontal scroll view with in the tableview cell ,that means add the the horizontal scrollview in "cellForRowAtIndexPath" method.
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int x = 13;
int y=13;
UIScrollView *horizontalScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
[horizontalScroll setContentSize:(CGSizeMake(790, 60))];
for (int i = 0; i < 8; i++) {
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(x, 0, 45, 22);
[button setTag:i];
[button setTitle:#"08:30" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:234.0/255 green:234.0/255 blue:234.0/255 alpha:1]];
button.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:13.0];
[horizontalScroll addSubview:button];
x +=62;
}
[cell addSubview:horizontalScroll];
}
Related
I have created UITableview Programmatically and Populated array values dynamically. Here My Problem is, I created a UIButton Programmatically for zeroth row, It created well but When I am Scrolling the tableview button is Populating some other cells also. I don't Know how to solve that issue in my tableview. I want a UIButton on Only Zeroth index of the cell. I have tried the below code..
- (IBAction)MoreBttn:(id)sender {
NSUserDefaults *userDefaults1 = [NSUserDefaults standardUserDefaults];
[userDefaults1 setObject:amenties forKey:#"FACILITIES"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
amentiestableview=[[UITableView alloc]initWithFrame:screenRect style:UITableViewStylePlain];
amentiestableview.delegate=self;
amentiestableview.dataSource=self;
UIEdgeInsets insets = UIEdgeInsetsMake(20,10,20,10);
CGRect tableframe=amentiestableview.frame;
amentiestableview.frame=UIEdgeInsetsInsetRect(tableframe,insets);
[[UIApplication sharedApplication].keyWindow addSubview:amentiestableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [amenties count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"facilities";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
else{
}
cell.textLabel.text=[amenties objectAtIndex:indexPath.row];
return cell;
}
-(void)hidetableview:(UIButton*)sender
{
amentiestableview.hidden=YES;
}
The cell is being re-used, so the button appear on other cells. Try to update cellForRow method as follows:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
if(indexPath.row == 0) {
b.hidden = false
}
else{
b.hidden = true
}
We are recycling the cells by using the method dequeueReusableCellWithIdentifier. And you should recycle cells because the allocation of views need a lot of resources
Please try the below code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
b.tag = 1010
[cell addSubview:b];
}
else{
cell.contentView.viewWithTag(1010).removeFromSuperview()
}
I wish to add radio button to UITableview. The function of radio button should be like when I select radio button in the table cell that radio button should be selected and other all radio buttons in the other cell should be unselected. It should be kind of either or, that is only one radio button can select.
create the one int value
int selectstr;
UIButton * button;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 14.0, 215.0, 36.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor=[UIColor blackColor];
title.tag=indexPath.row;
title.text = #"TEST";
// title.font = [UIFont fontWithName:#"Arial Hebrew" size:16.0f];
title.font = [UIFont fontWithName:#"Palatino-Roman" size:14.0];
[cell.contentView addSubview:title];
button = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 46.0f, 30.0f)];
button.tag=indexPath.row;
if (selectstr ==indexPath.row)
{
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor colorWithRed:(243.0/255.0) green:(114.0/255.0) blue:(74.0/255.0) alpha:1.0f];
}
else
{
[button setImage:[UIImage imageNamed:#"radio_unselected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor clearColor];
}
button.adjustsImageWhenHighlighted=NO;
[button addTarget:self action:#selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectstr=indexPath.row;
[tableView reloadData];
}
- (IBAction)toggleCheckedMode:(UIButton*)sender
{
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
// here customized for your self
selectstr=sender.tag;
[tableView reloadData];
}
Write this code into
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSIndexPath *selectedIndex;
UIButton *radioButton;
radioButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
radioButton.frame = CGRectMake(30,23,24,24);
// [radioButton setTitle:#"" forState:UIControlStateNormal];
radioButton.tag = 8888;
[cell.contentView addSubview:radioButton];
radioButton = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndex && selectedIndex.row == indexPath.row)
{
[radioButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
else
{
[radioButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex =indexPath;
[self.userTabelView reloadData];
}
I have a Table View that rows data display basis on the user settings.
Like I have check box for Task, Start Date, Priority , Process, State , Description Etc.
I user check Task and Description then I create UITableViewCell then Add Label for Task And Description if user check All the Settings then Cell should Display all the Label created.
What Problem I am facing is that UITableView Not Scrolling smoothly its not reusing the cell when user scrolls it second Time after Loading the settings.
Below is the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellMainNibID = #"cellMain";
NSUInteger count=[arrSettings count];
TaskItem* item=[self.arrTaskList objectAtIndex:indexPath.row];
CGFloat y=2;
UITableViewCell* cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellMainNibID];
if (_cellMain == nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellMainNibID];
for (int i=0; i<count; i++)
{
UILabel *lbll=(UILabel*)[cell.contentView viewWithTag:indexPath.row+ i+200];
NSLog(#" cell label = %#",lbll); //**Return null always**
UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, y, 310, 14)];
lbl.tag=indexPath.row+ i+200;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor whiteColor];
lbl.font=[UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:lbl];
lbl=nil;
NSString* str=[arrSettings objectAtIndex:i];
CGRect rect;
if ([str isEqualToString:#"description"])
rect=CGRectMake(8, y+14, 290, 50);
else
rect=CGRectMake(110, y, 290, 14);
UILabel *lblll=(UILabel*)[cell.contentView viewWithTag:indexPath.row+ i+400];
NSLog(#" cell label = %#",lblll); //**Return null always**
UILabel* lbl2=[[UILabel alloc]initWithFrame:rect];
lbl2.backgroundColor=[UIColor clearColor];
lbl2.tag=indexPath.row+i+400;
lbl2.textColor=[UIColor whiteColor];
lbl2.font=[UIFont systemFontOfSize:14];
lbl2.numberOfLines=5;
lbl2.lineBreakMode=NSLineBreakByCharWrapping;
[cell.contentView addSubview:lbl2];
y+=lbl2.frame.size.height+1;
lbl2=nil;
}
}
for (int i=0; i<count; i++)
{
UILabel* lbl=(UILabel*)[cell.contentView viewWithTag:indexPath.row+i+200];
lbl.text=[[[arrSettings objectAtIndex:i]uppercaseString]stringByAppendingString:#":"];
UILabel* lbl2=(UILabel*)[cell.contentView viewWithTag:indexPath.row+i+400];
lbl2.text=[item valueForKey:[arrSettings objectAtIndex:i]];
}
cell.contentView.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
_cellMain.contentView.backgroundColor=[UIColor clearColor];
_cellMain.backgroundColor=[UIColor clearColor];
UIButton *btnCheck=[UIButton buttonWithType:UIButtonTypeCustom];
btnCheck.tag=indexPath.row+300;
if ([arrSelectedRow containsObject:[NSNumber numberWithLong:btnCheck.tag-300]])
{
[btnCheck setImage:[UIImage imageNamed:#"cb_glossy_on#2x.png"] forState:UIControlStateNormal];
}
else
{
[btnCheck setImage:[UIImage imageNamed:#"cb_glossy_off#2x.png"] forState:UIControlStateNormal];
}
btnCheck.frame=CGRectMake(295, cell.center.y-22, 33, 33);
[btnCheck addTarget:self action:#selector(checkClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView=btnCheck;
btnCheck=nil;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger count=[arrSettings count];
CGFloat totalHeight=(16*count)+(count*2);
if (isDesc)
totalHeight+=60;
return totalHeight;
}
Could anyone please suggest me how to display the dynamic data in UITableView based on User Settings for UITableViewCell Labels ?
Change:
if (_cellMain == nil)
to
if (cell == nil)
Add following code in your cellForRowAtIndex method of UITableView
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
I've had the same problem with not smooth tableView scrolling. In my case the reason was in what I forgot to implement tableView method tableView: registerNib: forCellReuseIdentifier: (in that project I used .xib's to create cells).
[self.tableView registerNib:[UINib nibWithNibName:#"YourTableViewCell" bundle:nil] forCellReuseIdentifier:YourCellReuseIdentifier];
i am working with the tableview in which i added 2 buttons on one cell. Below is the code which i used
- (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];
}
cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
tickbtn.tag = 200+indexPath.row;
[tickbtn setBackgroundImage:[UIImage imageNamed:#"ok_gray.png"]forState:UIControlStateNormal];
[tickbtn addTarget:self action:#selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(220, 10, 30, 30);
[cell.contentView addSubview:tickbtn];
NSLog(#"tickbtn tag %ld",(long)tickbtn.tag);
crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
crossbtn.tag = 400+indexPath.row;
[crossbtn setBackgroundImage:[UIImage imageNamed:#"delete-gray.png"]forState:UIControlStateNormal];
[crossbtn addTarget:self action:#selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
crossbtn.frame = CGRectMake(250, 10, 30, 30);
[cell.contentView addSubview:crossbtn];
NSLog(#"tickbtn tag %ld",(long)crossbtn.tag);
return cell;
}
on the tickbtn and crossbtn i am applying following actions :-
-(IBAction)addshed:(UIControl *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-200 inSection:0];
UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];
UIButton *check1 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+200];
UIButton *check2 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+400];
UIImageView *btnimg1 = [[UIImageView alloc] initWithImage:check1.currentBackgroundImage];
//UIImageView *btnimg2 = [[UIImageView alloc] initWithImage:check2.currentBackgroundImage];
NSLog(#"SHED LIST subviews: %#", btnimg1.image);
// Shed_data *sheddata = [[Shed_data alloc] init];
if (btnimg1.image == [UIImage imageNamed:#"ok_gray.png"]) {
//btnimg.image = [UIImage imageNamed:#"ok_gray.png"];
[check1 setBackgroundImage:[UIImage imageNamed:#"ok_green.png"]forState:UIControlStateNormal];
[check2 setBackgroundImage:[UIImage imageNamed:#"delete-gray.png"]forState:UIControlStateNormal];
[self addsheddata:sender];
NSLog(#"tickbtn tag %ld",(long)tickbtn.tag);
}
else if (btnimg1.image == [UIImage imageNamed:#"ok_green.png"])
{
[check2 setBackgroundImage:[UIImage imageNamed:#"delete-red.png"]forState:UIControlStateNormal];
[check1 setBackgroundImage:[UIImage imageNamed:#"ok_gray.png"]forState:UIControlStateNormal];
[self removesheddata:sender];
}
}
-(IBAction)removeshed:(UIControl*)sender
{
//.…………………….. My functionality
}
but in both these cases i am getting the tag value of last cell only whenever i am pressing the buttons of the cell.
Please locate my error and help me out to solve it. Your help will be much appreciable.
Try this one as working fine for me. I Just tested with my Xcode 5.
Modification :
1. I Create an NSMutableArray with the name of _objects (_objects = [[NSMutableArray alloc]initWithObjects:#"one",#"two",#"thre", nil];). and give it to my UITableView.
2.Give the tickBtn and crossBtn an different color so easily visible.
3.change the button pressed function to UIControl to UIButton like -(IBAction)addshed:(UIButton *)sender and when button pressed i catch the tag value and print it out on the console.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
tickbtn.tag = 200+indexPath.row;
[tickbtn setBackgroundImage:[UIImage imageNamed:#"ok_gray.png"]forState:UIControlStateNormal];
[tickbtn setBackgroundColor:[UIColor blackColor]];
[tickbtn addTarget:self action:#selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(220, 10, 30, 30);
[cell.contentView addSubview:tickbtn];
NSLog(#"tickbtn tag %ld",(long)tickbtn.tag);
crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
crossbtn.tag = 400+indexPath.row;
[crossbtn setBackgroundImage:[UIImage imageNamed:#"delete-gray.png"]forState:UIControlStateNormal];
[crossbtn addTarget:self action:#selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
crossbtn.frame = CGRectMake(250, 10, 30, 30);
[crossbtn setBackgroundColor:[UIColor greenColor]];
[cell.contentView addSubview:crossbtn];
NSLog(#"tickbtn tag %ld",(long)crossbtn.tag);
return cell;
}
-(IBAction)addshed:(UIButton *)sender {
NSLog(#"add shed %d",sender.tag);
}
-(IBAction)removeshed:(UIButton *)sender {
NSLog(#"remove %d",sender.tag);
}
NEW QUESTION UPDATE
Did you try with 10 or more cells and try with some continuous scroll?
And the result is
As the Another Answer says
[cell addSubview:crossbtn];// -------- Change here ---------
Let me clear this as i know about it.
The contentView is a subview of UITableViewCell. please review this reference and here you can see there are actually 3 subviews in a UITableViewCell.
You need to add your button to cell subview not cell's contentview subview. So use this 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];
}
cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
tickbtn.tag = 200+indexPath.row;
[tickbtn setBackgroundImage:[UIImage imageNamed:#"ok_gray.png"]forState:UIControlStateNormal];
[tickbtn addTarget:self action:#selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(220, 10, 30, 30);
[cell addSubview:tickbtn];// -------- Change here ---------
NSLog(#"tickbtn tag %ld",(long)tickbtn.tag);
crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
crossbtn.tag = 400+indexPath.row;
[crossbtn setBackgroundImage:[UIImage imageNamed:#"delete-gray.png"]forState:UIControlStateNormal];
[crossbtn addTarget:self action:#selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
crossbtn.frame = CGRectMake(250, 10, 30, 30);
[cell addSubview:crossbtn];// -------- Change here ---------
NSLog(#"tickbtn tag %ld",(long)crossbtn.tag);
return cell;
}
Your code seems fine for cellForRowAtIndexpath:. Error might be in getting the tag value at button click. Try to change with this code:-
-(IBAction)addshed:(UIControl *)sender
{
//.…………………….. My functionality
int selectedRow1 = ((UIControl *)sender).tag;
NSLog(#"No. %d", selectedRow1);
}
I have seen issue. It may be lead to this type of error.
Why do you add subviews again and again to your cell's content view.?
That is, for every cellForRowAtIndexpath: call, button will be add to cell. In case dequeueReusableCellWithIdentifier:, your last cell may reuse to any other cell while scroll. It will lead to your error. that is your cell will contain two button.(tag with last cell and tag with your current cell).
In this line [cell.contentView addSubview:tickbtn];, you have to do some change according to add once and also for crossbtn.
Updation: I have seen your updated question. My suggestion, better use custom cell. Use this link to how to create custom cell.. Lot of confusion in your code. ex. in this line UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];, It will give unexpected. Don't call delegate method like this.
I have added 2 button in a row of table view, for all the rows, and these button clicked as it first time appear in table view , when I scroll the table list the button tapping disable,
here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ImageOnRightCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.userInteractionEnabled = NO;
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *finalPriceBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
int i=indexPath.row;
finalPriceBtn.backgroundColor=[UIColor redColor];
finalPriceBtn.tag=MAINLABEL_TAG;
finalPriceBtn.frame = CGRectMake(200, 0.0, 100, 50);
[finalPriceBtn addTarget:self action:#selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
finalPriceBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[finalPriceBtn setImage:[UIImage imageNamed:#"man.jpg"] forState:UIControlStateNormal ];
[cell.contentView addSubview:finalPriceBtn];
finalPriceBtn1.backgroundColor=[UIColor redColor];
finalPriceBtn1.tag=SECONDLABEL_TAG;
finalPriceBtn1.frame = CGRectMake(50.0, 0.0, 80.0, 45.0);
[finalPriceBtn1 addTarget:self action:#selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn1.titleLabel.font=[UIFont systemFontOfSize:12];
finalPriceBtn1.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[finalPriceBtn1 setImage:[UIImage imageNamed:#"bulk-female.jpg"] forState:UIControlStateNormal ];
[cell.contentView addSubview:finalPriceBtn1];
}
else
{
finalPriceBtn = (UIButton *)[cell.contentView viewWithTag:MAINLABEL_TAG];
finalPriceBtn1 = (UIButton *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
}
return cell;
}
It is happening because, each time you are scrolling the tableview, your cells are reused and in that case cell is not nil and the code above the code before the cell==nil, makes the userInteractionEnabled to NO. That's why, your button is not clickable.
First time those buttons are clickable because they were not allocated, I mean the cell was not allocated and setting any attribute to non-allocated entity makes no effect. Hope you got the point.