First off tags wont work. I say this because i create 4 buttons all with the same tag for a specific cell i.e indexPath.row = tag.
Inside My TableViewCellForRowAtIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"courseCell";
//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//step 2: If there are no cell to reuse, create a new one
if (cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
...
//-------------Creation of Custom Buttons-------------//
//-----img = "radioOn.png"-----//
//----img2 = "radioOff.png"----//
//----RadioButtonA----//
...
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonA addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
radioButtonA.tag=indexPath.row;
//----End RadioButtonA----//
//----RadioButtonB----//
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonB.tag =indexPath.row;
...
//----End RadioButtonB----//
//----RadioButtonC----//
radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonC addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonC.tag = indexPath.row;
...
//----End RadioButtonC----//
//----RadioButtonNA----//
radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
radioButtonNA.tag = indexPath.row;
[radioButtonNA addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
...
//----End RadioButtonC----//
//---------End of Radio Button Creations---------//
//---------UIStepper & StepperLabel Creation-----//
[cell.contentView addSubview:radioButtonA];
[cell.contentView addSubview:radioButtonB];
[cell.contentView addSubview:radioButtonC];
[cell.contentView addSubview:radioButtonNA];
//Step4: Return the cell
return cell;
}
#pragma mark - Buttons
- (void)radioButtonClicked:(UIButton *)sender
{
UIButton *myButton = sender;
// This Method and all the ones similar to this method is created to handle the UITouchUpInsideEvent that the user sends when pressing the radioButtons A-NA.
[radioButtons addObject:sender];
// Create an instance(object) of class NSIndexPath called indexPath and set its value the indexPath of the cell the user is currently in.
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
// Initialize two unique variables in order to check if the buttons being clicked are being referenced in the correct index.
int row = indexPath.row;
NSLog(#"Button is in row %d", row);
...
}
-(IBAction)button:(UIButton*)sender{
...
#try {
for (i=0; i<8; i++) {
if ([credits count ] ==0) {
break;
}
HERE is where i am trying to access the radiobuttons i created in my cell. What i would like to do is this
if([credits objectAtIndex:i]) == radioButtonA{
do stuff. The reason im not saying == [radioButtonA tag] is because i have three other buttons all with the same tag. If your read the code u see why the tags are set this way.
}
What I am asking for is 1 help, and 2 is there another way to check if two Buttons i.e objects are equal without having to rely on their tags.
Do not worry about the Try catch finally i was using it to catch the exception being thrown.
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"A"]) {
NSLog(#"radioA is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeA.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
NSLog(#"%f",gradeEarned);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"B"]) {
NSLog(#"radioB is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeB.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:#"C"]){
NSLog(#"radioC is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeC.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if([credits objectAtIndex:i]== radioButtonNA){
NSLog(#"boboboobbobob");
continue;
}
}
}
#catch (NSException *exception) {
NSLog(#"NSException Caught");
NSLog(#"Name: %#",exception.name);
NSLog(#"Reason: %#", exception.reason);
}
#finally {
NSLog(#"in finally block");
}
// if ([credits objectAtIndex: i] == defaulter) {
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Failed to select all grades" message:[NSString stringWithFormat:#"Your grade selections have been reset"] delegate:self cancelButtonTitle:#"great"otherButtonTitles:nil];
// [alert show];
// [alert release];
// [self refreshArray];
// }
NSLog(#"%f",gradeEarned);
if (gradeEarned == 0) {
textLabel.text= [NSString stringWithFormat:#"%f",gradeEarned];
}else {
NSLog( #"boob");
sum= (gradeEarned)/(creditHours);
NSLog(#"%f",sum);
textLabel.text= [NSString stringWithFormat:#"%f",sum];
//[self refreshArray];
}
}
For more information Here is the log...
NSLog(#"%#",[credits objectAtIndex:i]);
NSLog(#"%#",radioButtonA);
THE First output is the log of the [credits object atIndex:i]
UIButton: 0x6c91430; frame = (86 110; 32 30); opaque = NO; layer = CALayer: 0x6c914f0
2012-06-20 20:24:01.568 TableView[12557:f803] UIButton: 0x6ea8ad0; frame = (86 110; 32 30); opaque = NO; tag = 6; layer = CALayer: 0x6e746e0
As you can see The UIBUttons are DIFFERENT thus == operator does not work
When checking two object against one another you cannot use ==, you have to use [objectA isEqual:objectB], if those two objects are the same the answer will be YES and NO if they are not.
To read more go to: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html
and check what is written for isEqual:
I'm not sure, in the method,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
you should add the buttons only when the cell is nil.just like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"courseCell";
//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//step 2: If there are no cell to reuse, create a new one
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
//-------------Creation of Custom Buttons-------------//
//-----img = "radioOn.png"-----//
//----img2 = "radioOff.png"----//
//----RadioButtonA----//
...
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonA addTarget:self action:#selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
radioButtonA.tag=indexPath.row;
//----End RadioButtonA----//
//----RadioButtonB----//
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonB.tag =indexPath.row;
...
//----End RadioButtonB----//
//----RadioButtonC----//
radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonC addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonC.tag = indexPath.row;
...
//----End RadioButtonC----//
//----RadioButtonNA----//
radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
radioButtonNA.tag = indexPath.row;
[radioButtonNA addTarget:self action:#selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
...
//----End RadioButtonC----//
//---------End of Radio Button Creations---------//
//---------UIStepper & StepperLabel Creation-----//
[cell.contentView addSubview:radioButtonA];
[cell.contentView addSubview:radioButtonB];
[cell.contentView addSubview:radioButtonC];
[cell.contentView addSubview:radioButtonNA];
}
...
//Step4: Return the cell
return cell;
}
I had already known this, however, the purpose of this post was to see if their are any other ways. I also used the isMemberOfClass:[UIButton class] to further narrow down. Best way and most effective way of doing this is using the object's tags to compare against.
Related
I am having custom-cell in which i put the UIButton as the CheckBox.
I want the array of selected checkBox indexPath in ViewController.
I am trying to set the UIButton Action event into the ViewController but it is not accessible. Here is my Code,
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCellSaveSearchTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:#"SaveSearchCell"];
if(!cell)
{
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomTableViewCellSaveSearchTableViewCell" owner:self options:nil]objectAtIndex:0];
}
[self setText:cell];
[cell.btnDelete addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnDelete.tag = indexPath.row;
return cell;
}
-(void)btnClick : (UIButton *)btn
{
// I Can't accaes this method.
}
How can i get this.
UITableViewCells have a state of their own to reflect if they are selected or not. You don't need a button in the cells (it is discouraged for UX reasons anyway). Check here for a tutorial.
Do according to these three floowing method.
- (void)viewDidLoad {
[super viewDidLoad];
chekMarkArray = [[NSMutableArray alloc]init];
int totalData = 10;
chekMarkArray=[[NSMutableArray alloc] initWithCapacity:totalData];
for (int i=0; i<totalData; i++) {
[chekMarkArray addObject:#(0)];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *testButton;
if (cell == nil ||1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(100, 5,25, 25);
testButton.tag = indexPath.row;
BOOL selectindex = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
if (!selectindex) {
testButton.backgroundColor = [UIColor greenColor];
}else{
testButton.backgroundColor = [UIColor redColor];
}
[testButton addTarget:self action:#selector(DropDownPressed:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:testButton];
}
return cell;
}
checked button action
-(void)btnClick : (UIButton *)btn
{
UIButton *button = (UIButton*)sender;
BOOL currentStatus = [[chekMarkArray objectAtIndex:button.tag] boolValue];
//deselect all selection
for (int i=0;i<chekMarkArray.count;i++) {
[chekMarkArray replaceObjectAtIndex:i withObject:#(0)];//comment this line for multiple selected button
}
//select current button
[chekMarkArray replaceObjectAtIndex:button.tag withObject:#(!currentStatus)];
[self.tableView reloadData];
}
You may use this code instead of button
first Alloc NsmutableArray in ViewDidLoad
and put an Button outside Tableview to complete your action.
then add
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedRowsArray addObject:[arrShareTickets objectAtIndex:indexPath.row]];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedRowsArray removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I am having a radio button in table view cell. This is my radio button
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
if ([indexPath isEqual:selectedIndex])
{
newRadioButton.selected = YES;
}
else
{
newRadioButton.selected = NO;
}
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[table reloadData];
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
{
[sender setSelected:YES];
}
}
Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).
I need to make it look like the first image.
I think you should try:
in YourViewController.h
#interface YourViewController : UITableViewController {
NSIndexPath *selectedIndex
}
in YourViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
if ([indexPath isEqual:selectedIndex]) {
newRadioButton.seleted = YES;
} else {
newRadioButton.seleted = NO;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[tableView reloadData];
}
this link may help you
Unselect all the radio buttons before your if statement using something like this:
for (UITableViewCell *cell in [self visibleCells]) {
[[cell accessoryView] setSelected:NO];
}
When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property
you need to set the tag for everybutton inside cellForRowAtindexpth, something like
// where i is just a static int variable for holding the count
myRadioButton.tag = i ;
i=i+1
now you when you tap based on tag, you can check which button is pressed and you can select that one
to know which button is pressed u can know like this
-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
Try to reuse button
Solution: we can add & access controls by tag from view
if control found by tag we can use it.
if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
UIButton *newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:#"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:#"select"] forState:UIControlStateSelected];
cell.accessoryView = newRadioButton;
}
Hi in my Application I'm displaying the data form my local database sqlite in UITableView . In that data's I have integer value displaying in my UILabel. Now to want to increment and decrement the value by clicking button I have tried but its not working properly its only working one time when click the next row again its showing the old value please tell me how to resolve this one. I have created two UIButtons dynamically for increment and decrement.
My database data fetch code.
qtt=[[NSMutableArray alloc]init];
[self openDB];
NSString *sql =[NSString stringWithFormat:#"SELECT * FROM br"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement)== SQLITE_ROW) {
char *field5 = (char *)sqlite3_column_text(statement, 4);
NSString *field5Str =[[NSString alloc]initWithUTF8String:field5];
str2 = [[NSString alloc]initWithFormat:#"%#",field5Str];
[qtt addObject:str2];
}
}
My UITableView display code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =#"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle:#"+" forState:UIControlStateNormal];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=indexPath.row;
[cell.contentView addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle:#"-" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag=indexPath.row;
[cell.contentView addSubview:button1];
if([valueString isEqualToString:[self.qtt objectAtIndex:indexPath.row]])
{
int value=[valueString integerValue];
if (self.check==YES)
{
value=value+1;
str2=[NSString stringWithFormat:#"%d",value];
NSLog(#"%#",str2);
}
else
{
if (value==1)
{
}
else
{
value=value-1;
str2=[NSString stringWithFormat:#"%d",value];
NSLog(#"%#",str2);
}
}
cell.ttt.text=str2;
}
return cell;
}
My UIButton Action method code.
-(void)aMethod:(id)sender
{
self.check=YES;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData];
}
-(void)aMethod1:(id)sender
{
self.check=NO;
int tag=[sender tag];
valueString=[self.qtt objectAtIndex:tag];
[self.mytableview reloadData]:
}
The problem is that you keep creating buttons every time that you go through the tableView:cellForRowAtIndexPath: code. When you call reloadData the first time, enough cells are available for reuse, so your code adds new buttons on top of the old ones; this happens every time you click [+] or [-].
In order to resolve this issue, make creation of the buttons conditional. Give your buttons different tags - say, 100 for [+] and 101 for [-]. This would let you find a subview by tag, and avoid creating new buttons when they are already there. Since you need that tag in your aMethod: and aMethod1:, put the tag of indexPath.row on the cell itself, and use int tag = [sender.superview tag] expression to retrieve it.
Here is a skeleton of how you can implement this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier =#"Cell";
displayCell *cell =(displayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[displayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.ttt.text= [self.qtt objectAtIndex:indexPath.row];
if ([cell.contentView viewWithTag:100] == nil) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(130,25, 40.0, 40.0);
[button setTitle:#"+" forState:UIControlStateNormal];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=100;
[cell.contentView addSubview:button];
}
if ([cell.contentView viewWithTag:101] == nil) {
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(200,25, 40.0, 40.0);
[button1 setTitle:#"-" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag=101;
[cell.contentView addSubview:button1];
}
cell.contentView.tag = indexPath.row;
// Remove the increment/decrement code, it does not belong here
}
Your event handler methods would look like this:
-(void)aMethod:(id)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]+1;
[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
[self.mytableview reloadData];
}
-(void)aMethod1:(id)sender {
int tag=[[sender superview] tag];
int value=[[self.qtt objectAtIndex:tag] integerValue]-1;
if (value <= 0) value = 1;
[self.qtt replaceObjectAtIndex:tag withObject:[#(value) stringValue]];
[self.mytableview reloadData];
}
I have this collapsable UITableView where there is a UITextField and a UIButton in the last cell in each table section. I would like to send the text in the UITextField to the function that is called by the UIButton that is next to it in the same cell, but I am baffled in how to achieve this. Thanks in advance for the help!
- (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];
}
if ([self tableView:_tableView canCollapseSection:indexPath.section])
{
int num = 1;
if (self.chat[indexPath.section - 1][#"num"] != nil)
num = [self.chat[indexPath.section - 1][#"num"] intValue] + 1;
if (!indexPath.row)
{
cell.textLabel.text = self.chat[indexPath.section - 1][#"msg"]; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else if (indexPath.row < num && indexPath.row >= 1)
{
cell.textLabel.text = self.chat[indexPath.section - 1][key];
cell.accessoryView = nil;
}
else
{
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:field];
cell.accessoryView = nil;
}
}
else
{
cell.accessoryView = nil;
cell.textLabel.text = #"Normal Cell";
}
return cell;
}
Give some unique tag to textfield and button by below way:
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
self.sendButton.tag = indexPath.section *1000 + indexPath.row;
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
field.tag = self.sendButton.tag + 1;
[cell addSubview:field];
cell.accessoryView = nil;
Now on button event,
-(void) sendReply:(UIButton *)sender
{
UITextField *field = [YOOUR_TABLE_VIEW viewWithTag:sender.tag + 1];
//Do you coding here
}
Make a Custom UITableViewCell that has uitextfield and a button on it and make a protocol/delegate of that custom uitableviewcell you've created.. so you can have more control of your code and the event of your button in the future..
check this tutorial: http://www.codigator.com/tutorials/ios-uitableview-tutorial-custom-cell-and-delegates/
cheers
For this,
In cellForRowAtIndexPath: method where you are allocating the send button add one line just to give some identifier to send button as below,
[self.sendButton setAccessibilityIdentifier:[NSString stringWithFormat:#"%d#%d",indexPath.row,indexPath.section]];
Now, in sendReply: method,
//First get the row and section information
NSString *str=[sender accessibilityIdentifier];
NSArray *arr=[str componentsSeparatedByString:#"#"];
//Get the index path
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:[[arr objectAtIndex:0] intValue] inSection:[arr objectAtIndex:1] intValue]];
//get the cell
UITableViewCell *objCell=(UITableViewCell*)[tblviwBasketCell cellForRowAtIndexPath:iRowId];
Now objCell will be the cell in which you have added the button and text view. so get the subviews of objCell and access the textfield to get the text.
As you say, there is a text field in each section. You should set the tag of your UIButton and UITextField same as indexPath.section.
Then, in the target method, use this tag value to get the relevant cell from the relevant section, and iterate over it's subviews to get your textField.
In the target method, you should do something like this:
- (void) sendReply:(id)sender {
int section = [(UIButton*)sender tag];
int row = [myTableView numberOfRowsInSection:section] - 1;//assuming it is the last cell in the section that contains your textField.
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [myTableView cellForRowAtIndexPath:indexPath];
for (UIView* vu in cell.subviews) {
if ([vu isKindOfClass:[UITextField class]]) {
NSString* textString = [(UITextField*)vu text];
//perform any tasks you want with the text now
}
}
}
You can simply use viewWithTag:, since it does an iterative search, but all the extra code is to avoid iterating over all the cells before reaching your relevant cell.
Am display list of names in UITableview and I'm trying to select any one of the names by using radio button. The problem I face is I can't select the specific row of the cell, either all the rows are selected or deselected.
Code for displaying radio button in cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
int flag;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [user_name objectAtIndex:indexPath.row];
report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
if (flag == 0) {
[report_but setBackgroundImage:[UIImage imageNamed:#"radio_blank.png"] forState:UIControlStateNormal];
}
else
[report_but setBackgroundImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
[report_but addTarget:self action:#selector(radio_button:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:report_but];
return cell;
}
- (void)radio_button:(id)button
{
NSLog(#"radio button clicked");
NSLog(#"button tag %#",button);
if (flag == 0) {
flag = 1;
}
else
flag = 0;
[table_list reloadData];
}
While row 1 is selected
And I can't use didselectrowatindexpath method for this single row selection, since selecting the row should not select the radio button.
Can any one help me to select the only one row of this tableviewcell.
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *report_but;
if (cell == Nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *report_but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
report_but.frame = CGRectMake(260,18,20,20);
[report_but setTitle:#"" forState:UIControlStateNormal];
report_but.tag = 8888;
[cell.contentView addSubview:report_but];
}
report_but = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndexPath && selectedIndexPath.row == indexPath.row) {
} else {
}
}
and in radio_button:
- (void)radio_button:(id)button
{
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];
selectedIndexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
[table_list reloadData];
}