TableView stuck at scrolling in iPhone - iphone

I have create custom tablview_cell and add more button inside a cell in tableview. when i am running in simulator, its working good. but in device little stuck to scroll. Is there any way to solve this issue, please let me know.
Thanks Advance
I try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 250;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * identifier = #"Cell+Identifier";
Custom_Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Custom_Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.btn1 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn2 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn3 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn4 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn5 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn6 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn7 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn8 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn9 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn10 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn11 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn12 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
}
cell.label.text = [NSString stringWithFormat:#"%d",indexPath.row];
return cell;
}
-(void)selected_files:(id)sender
{
View_2 *v2 = [[View_2 alloc]init];
[self.navigationController pushViewController:v2 animated:YES];
}
Below i have mentioned the Custom_Cell for your reference.

Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cell";
Custom_Cell *cell = (Custom_Cell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"Custom_Cell" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (Custom_Cell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.btn1 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn2 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn3 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn4 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn5 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn6 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn7 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn8 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn9 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn10 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn11 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn12 addTarget:self action:#selector(selected_files:) forControlEvents:UIControlEventTouchUpInside];
cell.label.text = [NSString stringWithFormat:#"%d",indexPath.row];
return cell;
}

Related

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

i have created two button programmatically in UITableView i.e Edit Delete
when we click the cell these buttons are displayed but when i try to click in edit or delete button it doesn't calls the appropriate method i.e edit or deleteBtn.
This is my code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[[UIButton alloc]init];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
return cell;
}
my edit and delete function are very simple
-(void)edit{
NSLog("%#",selectedValue);
}
-(void)deleteBtn{
NSLog("%#",selectedValue);
}
i have checked this function applying breakpoint but it is not called.
this is how my selectedValue comes
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedValue=[firstName objectAtInder:indexPath.row];
}
Thanks, in advance,
Arun.
Think,the problem is with UIButton frame's.
in your code both button's origin.y is 100.0,so it went beyond the cell's bound (Default height is 44.0).
Change your Both UIButton frame like this,and it is worked for me.
UIButton * edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
UIButton* delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
please your code like below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:#selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];
delete=[[UIButton alloc]init];
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:#selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];
return cell;
}
use methods like below
-(IBAction)edit:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%#",str);
}
-(IBAction)deleteBtn:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%#",str);
}
Your buttons are being added over and over to the cell because they're being created every time you call CFRAIP. Move them into the cell == nil block:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[delete setTitle:#"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:#selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
return cell;
}
The problem is in the setFrame
The height of the table cell is less than the co-ordinates you have set for the buttons.
Change the code to
[edit setFrame:CGRectMake(100, 5, 100, 20)];
and this will work.
OR
just increase the height of the table rows, which by default is 44, using
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Please try to use this one ....and do same thing for delete button ..........
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// -------------------- Add button to cell --------------
edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];
[edit setTitle:#"Edit" forState:UIControlStateNormal];
[edit setTag:1];
[edit addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:edit];
}
I can you try to custom cell and can you add buttons in custom cell view and implement below codes.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//****cell recent deal buy button click goes to login or check out pageview
//[cell.btn setTag:indexPath.row];
//[cell.btn addTarget:self action:#selector(buybutton_checkout:)
// forControlEvents:UIControlEventTouchDown];
// cell.btn.hidden=NO;
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"icel" owner:nil options:nil];
for (UIView *view in views)
{
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (icel*)view;
}
}
}
//tbl.layer.cornerRadius = 3.9;
//[tbl setClipsToBounds:YES];
[cell.activity startAnimating];
[cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
[cell.btn setTag:indexPath.row];
[cell.btn addTarget:self action:#selector(Buy_btnlck:)
forControlEvents:UIControlEventTouchDown];
//cell.btn.hidden=NO;
cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];
index_tbl=indexPath.row;
return cell;
}
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:#"%ld",(long int)[button tag]]intValue];
}
Don't use [UIButton alloc] init]. Use instead this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(edit:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Edit" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];
Where view is the view where you are adding your button as a subview. you can also set Button frame according to your requirements.
Pls change the button names , as Edit and delete are already defined keywords in Objective C
Modify your code as like this....
Replace the below two lines...
edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];
as like this...
UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];
Try this::
Table Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell.
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
[btnEdit setTitle:#"Edit" forState:UIControlStateNormal];
[btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
[btnEdit setTag:indexPath.row];
[btnEdit addTarget:self action:#selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnEdit];
UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
[btnDelete setTitle:#"Delete" forState:UIControlStateNormal];
[btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
[btnDelete setTag:indexPath.row];
[btnDelete addTarget:self action:#selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnDelete];
}
return cell;
}
Then, Button Action Methods
-(IBAction)clickEdit:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %#",str);
}
-(IBAction)clickDelete:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %#",str);
}
Hopefully, it'll help you.
Thanks.

UITabel View Cell Button Mixed Up

In my table View Cell Each Cell Has two Button as, Edit And Cancel, On click Of Edit at The Same time Cancel Button Should Change. My Problem Is When I Try To click Edit Button Another cell cancel button image was changed not on that cell which i clicked?
Here Is My Code----
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
//_checkButton.tag = 0;
[cell.contentView addSubview:_checkButton];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// _cancelButton.tag = 1;
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
[cell addSubview:_cancelButton];
[cell addSubview:_textFieldQuantity];
} else {
cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:0];
cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:1];
}
[_checkButton setTag:indexPath.row];
[_cancelButton setTag:indexPath.row];
cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
cell._amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
return cell;
}
Thanks In Advance!!
For each cell button, give the tag of the current indexPath.row for ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
_checkButton.tag = [NSString stringWithFormat:#"5%d",indexPath.row];
[cell.contentView addSubview:_checkButton];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
_cancelButton.tag = [NSString stringWithFormat:#"6%d",indexPath.row];
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
[cell addSubview:_cancelButton];
[cell addSubview:_textFieldQuantity];
} else {
cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:#"5%d",indexPath.row]];
cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:#"6%d",indexPath.row]];
}
cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
cell._amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
return cell;
}

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

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

Adding Radio Button in Custom Cell of TableView and unable to access outside

I am adding Radio Buttons in custom cell of UITableView and calling a method on it to changes its image or background Image. I able to set Selected button Image from sender but I want to make other buttons to their original position which I am unable to set it, as below functionality need to be look like proper Radio buttion in a cell
I am also facing problem in fetching proper Index Path in this method - (void) TableRadioButtonSelection:(id)sender;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *RadioIdentifier = #"RadioCellIdentifier";
RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
if(myRadiocell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"RadioBtnCell" owner:self options:nil];
myRadiocell = radioCell;
//[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn1.tag = ButtonTag;
//[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn2.tag = ButtonTag1;
//[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn3.tag = ButtonTag2;
myRadiocell.tag = RadioTag;
[myRadiocell.radioBtn1 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn2 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn3 addTarget:self action:#selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
self.radioCell = nil;
}
return myRadiocell;
}
- (void) TableRadioButtonSelection:(id)sender {
//RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];
[mybtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn1 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[mybtn2 setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
//NSLog(#"%#",[cell subviews]);
UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];
// I am unable to set Image of other buttons apart from sender.
//for(UIView *item in [mysubview subviews]) {
if ([mysubview isKindOfClass:[UIButton class]]) {
UIButton *newBtn = (UIButton*)mysubview;
UIButton *newBtn1 = (UIButton*)mysubview1;
UIButton *newBtn2 = (UIButton*)mysubview2;
NSLog(#"OK %#",newBtn);
//[newBtn setBackgroundImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn1 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[newBtn2 setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
//}
NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell];
NSLog(#"%d",indexPath.row);
// Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image
UIButton *btn = (UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:#"radiobtn_chk.png"] forState:UIControlStateNormal];
}
try changing this line to this;-
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
to
RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];
In this way you can access the table view cell.After that you can get the button from cell content views and do further coding.
Suppose if you do not know the indexPath then you can create the indexPath fromm the rowNumber of the table view(I think you have stored the table row number in RadioTag, if yes so you can use this field as a parameter).

How to add image and buttons in UITableView in iPhone

I have to add one image, one label and one button in each cell. How will I add these 3 different controls in a UITableview?
Thanks in advance
You should do 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];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[cell.contentView addSubview:button];
cell.imageView.image = <you image>;
cell.textLabel.text = <your text>;
}
Both of the previous answers do not handle this correctly as they
Do not actually create/get a cell
Add subview directly to cell when you should normally add to the content view cell.contentView
I did try to edit the original answer but no one has accepted it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[cell addSubview:button];
}
//Like this you can add the Label and the image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setImage:[UIImage imageNamed:#"normallook.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"highlightimage.png"] forState:UIControlStateHighlighted];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[cell addSubview:button];
}