Hi All,
- (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];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
textField.tag = 123;
textField.placeholder = #"Enter Text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.delegate = self;
[cell.contentView addSubview:textField];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
if (indexPath.row == 0) {
[textField setPlaceholder:#"Employee ID"];
}
else if (indexPath.row == 1)
{
[textField setPlaceholder:#"Employee Name"];
}
else if (indexPath.row == 2)
{
[textField setPlaceholder:#"Employee Phone"];
}
if (indexPath.row == 3)
{
[textField setPlaceholder:#"Employee Email"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[saveButton setFrame:CGRectMake(200, 0, 100, 40)];
[saveButton setTitle:#"Save Emp" forState:UIControlStateNormal];
[saveButton addTarget:self action:#selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
[saveButton setTag:indexPath.section];
[cell addSubview:saveButton];
}
return cell;
}
Hi Everyone,
I am using this piece of code for getting the above output
But when i am scrolling the tableview the output i am getting is
And if i enter any text in the section and scrolling the text, then the text is changing in the cells.
You are adding a subview to the cell in one of the cases:
[cell addSubview:saveButton];
This subview does NOT get removed when you dequeue an old cell. You have to explicitly remove the subview for those cases. This will cause unexpected behaviour.
I really recommend to subclass UITableViewCell and add the components to that subclass. by doing so, you can hide its saveButton-property for the cases where you do not want a saveButton.
Initialize your UITextField and UIButton with tag in
if (cell == nil)
{
//Initialize your `UITextField` and `UIButton`
// also set tag
}
And set frame of UITextField and UIButton at out of if statement. (you can get UITextField and UIButton by its tag)
sure its working for you.
I too had the same problem,try this,
UITableViewCell *cell;
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//initialize the tableviewcell outside,
You need to use the custom UITableViewCell & in that class file, you need to get this tied up in the cell Class file . More importantly, need to maintain the datasource for it .
Don't use the tag as 0. I mean, set something similar for every views
txtView.tag = 25+indexPath.section;
Change this "textField.tag = 123" to "text.tag = indexPath.row" in all places. It will work properly.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (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];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
textField.tag = 123;
textField.placeholder = #"Enter Text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.delegate = self;
[cell.contentView addSubview:textField];
} else {
//here the cell is reused, so remove the button here
[[cell.contentView viewWithTag:124] removeFromSuperview];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
if (indexPath.row == 0) {
[textField setPlaceholder:#"Employee ID"];
}
else if (indexPath.row == 1)
{
[textField setPlaceholder:#"Employee Name"];
}
else if (indexPath.row == 2)
{
[textField setPlaceholder:#"Employee Phone"];
}
if (indexPath.row == 3)
{
[textField setPlaceholder:#"Employee Email"];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[saveButton setFrame:CGRectMake(200, 0, 100, 40)];
[saveButton setTag:124];
[saveButton setTitle:#"Save Emp" forState:UIControlStateNormal];
[saveButton addTarget:self action:#selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
//[saveButton setTag:indexPath.section];
[cell.contentView addSubview:saveButton];
}
return cell;
}
As per your screen shot there are three visible cell. so when ever you load 4th cell ios will give you reference of 1st cell. here you have already set button for employee email. so that it will visible and again you also set employee id and phone.
So when you reuse the cell your logic should like you need to reset the cell to the default case in this example you can remove that button or hide it and then as per the cell info you should set the cell properties.
I hope you understand the problem.
if (indexPath.row == 3)
{
[textField setPlaceholder:#"Employee Email"];
UIButton *saveButton = [cell.contentView viewWithTag:124];
if(saveButton == nil)
saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[saveButton setFrame:CGRectMake(200, 0, 100, 40)];
[saveButton setTag:124];
[saveButton setTitle:#"Save Emp" forState:UIControlStateNormal];
[saveButton addTarget:self action:#selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
//[saveButton setTag:indexPath.section];
[cell.contentView addSubview:saveButton];
}
Try this code it reuse cells in table view
- (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];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, 200, 30)];
textField.tag = 123;
textField.placeholder = #"Enter Text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.delegate = self;
[cell.contentView addSubview:textField];
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[saveButton setFrame:CGRectMake(200, 0, 100, 40)];
[saveButton setTitle:#"Save Emp" forState:UIControlStateNormal];
[saveButton addTarget:self action:#selector(saveEmployeeToCoreData:) forControlEvents:UIControlEventTouchUpInside];
[saveButton setTag:124];
[cell.contentView addSubview:saveButton];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:123];
UIButton *saveButton =(UIButton*)[cell.contentView viewWithTag:124];
saveButton.hidden=YES;
if (indexPath.row == 0) {
[textField setPlaceholder:#"Employee ID"];
}
else if (indexPath.row == 1)
{
[textField setPlaceholder:#"Employee Name"];
}
else if (indexPath.row == 2)
{
[textField setPlaceholder:#"Employee Phone"];
}
if (indexPath.row == 3)
{
[textField setPlaceholder:#"Employee Email"];
saveButton.hidden=NO;
}
return cell;
}
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 have a custom table view in my app which has button in each row.
I want to change button background image when flag of that cell is i'1'(check if statement in code.).
it is successfully executing if statements.
Just the button's background image is not changing.
Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell= [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"]autorelease];
if(tableView == tablePatchFilter)
{
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *typeLabel = [[[UILabel alloc]init]autorelease];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text = [displayItemsType objectAtIndex:indexPath.row];
typeLabel.textAlignment = NSTextAlignmentLeft;
typeLabel.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel.frame = CGRectMake(300,2.0f,250,50);
[cell addSubview:typeLabel];
UILabel *typeLabel1 = [[[UILabel alloc]init]autorelease];
typeLabel1.backgroundColor = [UIColor clearColor];
typeLabel1.text = [displayItemPatch objectAtIndex:indexPath.row];
typeLabel1.textAlignment = NSTextAlignmentLeft;
typeLabel1.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel1.frame = CGRectMake(550,2.0f,250,50);
[cell addSubview:typeLabel1];
UILabel *typeLabel2 = [[[UILabel alloc]init]autorelease];
typeLabel2.backgroundColor = [UIColor clearColor];
typeLabel2.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel2.text = [displayItemclass objectAtIndex:indexPath.row];
typeLabel2.textAlignment = NSTextAlignmentLeft;
typeLabel2.frame = CGRectMake(730,2.0f,250,50);
[cell addSubview:typeLabel2];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0)
{
for(int i = 0;i<[displayFlag count];i++)
{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
NSLog(#"XXXXXXXXXXXXXXXXXXXXXXXXXX%#",pathhh);
if([pathhh isEqualToString:#"1"])
{
NSLog(#"in IFFFFFFF%#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
}
else
{
NSLog(#"in ELSSSSSSSSSS %#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"]
forState:UIControlStateNormal];
}
}
/*
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
*/
}
}
cell.textLabel.font=[UIFont fontWithName:MyFont size:17.f];
return cell;
}
In your implementation every time you are creating new cell, after getting cell from "dequeueReusableCellWithIdentifier" you have to check for cell if it nil than only create new cell using "alloc-init".
The better implementation of the particular senireo is to take a custom UITableViewCell.
The following stategie you should follow.
In the custom cell create the outlet for label 1, 2 and 3, than set the test in cellForRowatIndexPath.
Create a property of NSMutableArray in the custom cell class.
After getting the cell from "dequeueReusableCellWithIdentifier" check if cell is nil than only create the buttons and add all the buttons in the NSMutableArray created in step 2.
take the buttons from the NSMutableArray and that apply the condition for setting the images.
Please see the below code:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == tablePatchFilter)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
return cell;
} else {
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(takeSignature:) forControlEvents:UIControlEventTouchUpInside];
[cell.buttonArray addObject:btn];
}
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label1.text = [displayItemsType objectAtIndex:indexPath.row];
cell.label2.text = [displayItemPatch objectAtIndex:indexPath.row];
cell.label3.text = [displayItemclass objectAtIndex:indexPath.row];
for (UIButton *btn in cell.buttonArray) {
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
if([pathhh isEqualToString:#"1"]) {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"] forState:UIControlStateNormal];
} else {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
}
}
return cell;
}
}
UITable view is working fine. when scrolling checked image is changed to uncheck. And also i need to take the particular checked image data in SAVE button Action.Can any body help me to solve this problem in prj.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal]; [cell addSubview:btn];
btn.tag=4;
forState:UIControlStateNormal];
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:#"unchecked.png"]])
{
[sender setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:#"unchecked.png"]forState:UIControlStateNormal];
}
}
Take one array which is Use to store IndexValue of Selected button,
#property (nonatomic,retain) NSMutableArray *arySelected;
Initialize array in ViewDidLoad,
- (void)viewDidLoad {
[super viewDidLoad];
self.arySelected = [[NSMutableArray alloc] init];
}
Change Code of TableViewCell Delegate,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
if (![arySelected containsObject:[NSString stringWithFormat:#"%ld",(long)indexPath.row]])
{
[btn setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateSelected];
}
else
{
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
// On your button Event,
-(void)checkBoxClicked:(id)sender
{
sender.selected = ! sender.selected;
if (sender.selected)
{
[arySelected addObject:[NSString stringWithFormat:#"%ld",(long)sender.tag]];
}
else
{
[arySelected removeObject:[NSString stringWithFormat:#"%ld",(long)sender.tag]];
}
[self.tableView reloadData];
}
you use the deferent state to set image like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle= UITableViewCellSelectionStyleNone;
}
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
[btn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed: #"checked.png"] forState:UIControlStateSelected];
btn.selected=NO;
btn.tag=4;
[btn addTarget:self action:#selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
lbl_name.text=[NSString stringWithFormat:#"%#",[[arr1 valueForKey:#"Name"]objectAtIndex:indexPath.row]];
lbl_name.tag=5;
lbl_name.textColor=[UIColor blackColor];
[lbl_name setTextAlignment:NSTextAlignmentCenter];
lbl_name.font=[UIFont systemFontOfSize:15];
[cell addSubview:lbl_name];
return cell;
}
-(void)checkBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
if (tappedButton.isSelected) {
tappedButton.selected=NO;
}
else
{
tappedButton.selected=YES;
}
}
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 grouped UITableView (this one has 4 sections) and several rows each. I have programatically created 2 buttons in each of the cells.
This is how I am creating the UITableViewCell. In this code, I am trying to detect the indexPath.row and the indexPath.section of the button that was pressed and pass it to the method. How am I able to do this ?
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"Cell";
UITableViewCell *cell ;//= [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSString *docTitle = [currentDocument objectForKey:#"displayname"];
UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(cellView.frame.origin.x + 5, cellView.frame.origin.y + 5, cellView.frame.size.width - 10, 25)];
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.text = docTitle;
[cellView addSubview:cellTitle];
[cell.contentView addSubview:cellView];
UIButton *viewDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewDocumentButton setTitle:#"View Online" forState:UIControlStateNormal];
viewDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, cellTitle.frame.origin.y + cellTitle.frame.size.height + 5, 150, 35);
[viewDocumentButton addTarget:self action:#selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[viewDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:viewDocumentButton];
UIButton *downloadDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadDocumentButton setTitle:#"Download Document" forState:UIControlStateNormal];
downloadDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, viewDocumentButton.frame.origin.y + viewDocumentButton.frame.size.height + 5, 150, 35);
[downloadDocumentButton addTarget:self action:#selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[downloadDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:downloadDocumentButton];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Additionally, you could...
- (IBAction)openDocumentButtonPressedMethod:(id)sender {
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UITableViewCell *cell = (UITableViewCell *)contentView.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do something with the indexPath
}
And if you wanted to use the same method to handle both button events, then you could use the tag on the button to distinguish between the two buttons.
if (button.tag == 1) {
// do something with the indexPath view related
}
else {
// do something with the indexPath download related
}
Create a subclass of UIButton
#interface MyButton : UIButton
{ /* indexPath, other stuff */ }
#end
Then implement
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
to do something with the indexPath and other stuff before invoking the super for this method.
When you create the button, provide indexPath.
In your openDocumentButtonPressedMethod: method, do
CGPoint pointInTableView = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableView];