-(void)sendcomment:(UIButton *)sender{
NSLog(#" send commment server");
thirdviewcellTableViewCell *dja =[[thirdviewcellTableViewCell alloc] init];
NSString *vga = [[NSString alloc] init];
vga=dja.celltext;
NSLog(#"comment value is %#",vga);
NSLog(#"comment cell value is %#",dja.celltext);
}
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)[tblView cellForRowAtIndexPath: indexpath];
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
If the cell hold the button, you can do this:
-(void)sendcomment:(UIButton *)sender{
// 1. get the position the button you clicked
CGPoint correctedPoint = [sender convertPoint:button.bounds.origin toView:self.tableView];
// 2. find the cell via the position
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
thirdviewcellTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];;
// 3. then you can get the TextField value
NSString *textFieldValue = [[NSString alloc] init];
textFieldValue = cell.celltext;
}
you get your cell using cell's hierarchy
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview;// get cell using view hierarchy
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
*
In your action method you can Find Your TextField as below :
You can find IndexPath from SuperView.
See below Code.
UIView *superView = btn.superview;
while (![superView isKindOfClass:[UITableViewCell class]]) {
superView = superView.superview;
}
CustomCell *cell = (CustomCell *)superView;
You will get UITextField object from cell.yourTextFeld .
And you can use TextField as per your requirement.
Related
I have developed a UITableView with some cells that could have subcells.
Example:
My app allows users to move cells position but, when a table has got one or more cell with subcells, I collapse all of them in order to allow only move cells with the same identation (or in the same level)
If the table is pretty long, when I collapse cells with subelements, I use indexPath of that cell to remove the subelements from the table. My problem is that some of that cells are not in memory so I get a crash.
How can I detect that a cell is not being showed in order only remove the element from the dataSource?
This is my method to collapse/expand elements:
- (void)expandCollapseSubtasks:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:_taskTableView]; //when CGPoint = 0,0 not remove cells?
NSIndexPath *indexPath = [_taskTableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil)
{
Task *task = [_tasks objectAtIndex:indexPath.row];
TaskTitleCell *cell = (TaskTitleCell *)[_taskTableView cellForRowAtIndexPath:indexPath];
UILabel *subtasksNumberLabel = (UILabel *)[cell viewWithTag:107];
UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];
NSMutableArray *subtasksIndexPaths = [[NSMutableArray alloc] init];
NSNumber *hidden;
//Expand
if (!subtasksButton.selected){
hidden = #0;
subtasksNumberLabel.textColor = [UIColor colorWithRed:72.0/255.0 green:175.0/255.0 blue:237.0/255.0 alpha:1.0];
NSDictionary *subtasksAndIndexesDictionary = [task getSubtasksIndexesInTaskCollection:_tasks ofList:task.list];
NSIndexSet *indexes = [subtasksAndIndexesDictionary objectForKey:#"indexes"];
NSArray *subtasks = [subtasksAndIndexesDictionary objectForKey:#"subtasks"];
[_tasks insertObjects:subtasks atIndexes:indexes];
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSIndexPath *subtaskIndexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[subtasksIndexPaths addObject:subtaskIndexPath];
}];
[_taskTableView insertRowsAtIndexPaths:subtasksIndexPaths withRowAnimation:UITableViewRowAnimationTop];
//Collapse
}else{
hidden = #1;
subtasksNumberLabel.textColor = [UIColor whiteColor];
NSArray *subtasks = [task getSubtasks];
[_tasks removeObjectsInArray:subtasks];
for (int i=1;i<=subtasks.count; i++){
NSIndexPath *subtaskIndexPath = [NSIndexPath indexPathForRow:indexPath.row+i inSection:0];
[subtasksIndexPaths addObject:subtaskIndexPath];
}
[_taskTableView deleteRowsAtIndexPaths:subtasksIndexPaths withRowAnimation:UITableViewRowAnimationBottom];
}
subtasksButton.selected = !subtasksButton.selected;
task.hidesubtasks = hidden;
[[NSManagedObjectContext defaultContext] saveToPersistentStoreAndWait];
}
}
And the call
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
UITableViewCell *cell = [_taskTableView cellForRowAtIndexPath:indexPath];
UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];
[self expandCollapseSubtasks:subtasksButton];
Where idx is the position of the element in the dataSource
You can get array of NSIndexPath for all visible cells:
NSArray *array = [self.tableView indexPathsForVisibleRows];
You can also get array of cells itself instead of NSIndexPath:
[tableView visibleCells]
That what you need to find out the invisible cells.
I am trying to reach button indexpath in a correctly filled UITableView. I have two labels and one button in every cell customized in anouther XIB. When I click the button in any row in any section, it returns mw wrong indexpath. I am wondering what may cause this. Below you can find my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO]; //
NSString* strTemp = [NSString stringWithFormat: #"%d/%d", indexPath.section, indexPath.row ];
for (int i = 0; i < buttonClickedArray.count; i++) {
if([[buttonClickedArray objectAtIndex:i] isEqualToString:strTemp])
{
[cell.btnHatirlat setBackgroundColor:[UIColor colorWithRed:0.0 green:0.5 blue:0. alpha:0.7]];
[cell.btnHatirlat setTitle:#"Eklendi" forState:UIControlStateNormal];
}
}
NSString *str = #"";
str = [arrayPazartesi objectAtIndex:indexPath.row];
if ([str length] > 1)
{
NSString *strSaat = [str substringToIndex:5];
NSString *strEtkinlikAdi = [str substringFromIndex:6];
cell.lblEtkinlik.text = strEtkinlikAdi;
cell.lblSaat.text = strSaat;
[cell.btnHatirlat addTarget:self action:#selector(hatirlat:)
forControlEvents:UIControlEventTouchUpInside ];
}
return cell;
}
- (void) hatirlat:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableProgram];
NSIndexPath *indexPath = [self.tableProgram indexPathForRowAtPoint:buttonPosition];
int j = indexPath.section; //true section
int i = indexPath.row; //wrong row
}
I've seen this problem before (but can't find the reference), and the fix was to not pick the corner of the button (I think this would only be a problem if the button is right at the top of a cell). Instead of,
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableProgram];
Try this to make sure you're not right on an edge,
CGPoint boundsCenter = CGRectOffset(sender.bounds, sender.frame.size.width/2, sender.frame.size.height/2).origin;
CGPoint buttonPosition = [sender convertPoint:boundsCenter toView:self.tableView];
Try using this instead. I have seen quite a few people complain about this method behaving quite weirdly.
CGPoint point = [self.tableView convertPoint:sender.frame.origin fromView:sender.superview];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
Edit
The preferred way to do this would be to have the cell create the button (maybe during the init). And define a protocol for this cell, that returns a reference to the cell. Set the cell as the button delegate and when the button is clicked, pass a reference to the cell back to the cell delegate, which can call
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
e.g.
UITableViewCell.h
#protocol MyCellDelegate <NSObject>
#optional
- (void)cell:(UITableViewCell *)cell buttonPressed:(UIButton *)button;
#end
#interface MyCell : UITableViewCell
#property (weak, nonatomic) id<MyCellDelegate> delegate;
UITableViewCell.m
- (void)setDataWithDictionary:(NSDictionary *)dictionary
{
[self.titleLbl setText:[dictionary objectForKey:#"title"]];
[self.amountLbl setText:[dictionary objectForKey:#"amount"]];
btn = [[UIButton alloc] init];
[btn addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
.....
}
- (void)buttonPressed
{
if(self.delegate)
{
if([self.delegate respondsToSelector:#selector(cell:buttonPressed:)])
{
[self.delegate cell:self buttonPressed:btn];
}
}
}
Why don't you set the elements index inside the UIButtons tag inside cellForRowAtIndexPath ?
Everytime you push the UIButton hatirlat will be called and you can access the index by the sender.
Maybe it will cause a problem because of your two UITableViews but I don't know exactly your code.
- (void) hatirlat:(UIButton *)sender{
UIButton *button = (UIButton *)sender;
_arrayForPosition[button.tag];
}
I have a UITableView and each UITableViewCell has a textfield and UILabel. How can I update the label string on a cell when the textfield in the corresponding cell has been edited.
I tried something like this, but it's not working
UITextField *myTextField = (UITextField *)[cell viewWithTag:100];
UILabel *myLabel = (UILabel *)[cell viewWithTag:101];
[myTextField addTarget:self action:#selector(textFieldEdited:event:) forControlEvents:UIControlEventEditingDidEnd];
- (void)textFieldEdited:(UITextField*)textfield event:(UIEvent*)event
{
NSIndexPath* indexPath = [myTable indexPathForRowAtPoint:
[[[event touchesForView:textfield] anyObject]
locationInView:cartTable]];
NSLog(#"%d",indexPath.row);
UILabel *myLabel = (UILabel *)[[myTable cellForRowAtIndexPath:indexPath] viewWithTag:101];
[myLabel setText:#"44"];
}
But it didn't work.
Try this.. This may help you.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UITableviewCell *currentCell = (UITableviewCell *) [textField superView];
UILabel *label = [currentCell viewWithTag:YOURLABELTAG];
label.text = [NSString stringWithFormat:#"%#%#",textField.text,string];
return YES;
}
Note: don't forget to set textFieldDelegate and set label Tag
In your textFieldEdited: update your data source and reload the table data
[myTable reloadData];
Handle the changes then in your cellForRowAtIndexPath: method to return the updated cell.
CGPoint location = [tableview convertPoint:yourtextfield.frame.origin fromView:yourtextfield.superview];
NSIndexPath *indexPath = [tblItemList indexPathForRowAtPoint:location];
UITableview *cell = [self.table cellForRowAtIndexPath:indexPath];
for (UIView *subView in cell.subviews)
{
if ([subView isKindOfClass:[UILabel class]])
{
[(UILabel *) subView setText:#"Hearts"];
}
}
for Finding indexpath of cell is simple one line code
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
I want to get UITableviewCell value on click of Login button. When user press on Login button then i want to fetch first cell(Email) and second cell(password) value. Please help me.
Thanks!
Shailesh
Please try to use this one.I hope it may help you. But you should assign tag 1 to email and 2 to password text field.
-(IBAction)loginBtnPressed:(IBAction)sender
{
UITableViewCell *emailCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITableViewCell *passwordCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] ;//pass
UITextField *emailTxtFLd = (UITextField *)[emailCell viewWithTag:1];
UITextField *passwordTxtFLd = (UITextField *)[passwordCell viewWithTag:2];
NSLog(#"Email : %#",emailTxtFLd.text);
NSLog(#"Password : %#",passwordTxtFLd.text);
}
You do one thing, when you add textFiled as a subView to the cell, set a tag to the textFiled like as given,
[textField setTag:-1];
And when you want to get the text from the textFiled, follow the steps below.
// Getting email
CustomCell *tableCell = (CustomCell*)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UITextField *myTextField = (UITextField *)[tableCell viewWithTag:-1];
NSString *emailString = [myTextField text];
NSLog(#"~~~~ email: %#", emailString);
// Getting Password
tableCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
myTextField = (UITextField *)[tableCell viewWithTag:-1];
NSString *passwordString = [myTextField text];
NSLog(#"~~~~ password: %#", passwordString);
Hope this will help you :)
Since both cells are visible, you can just ask for them this way:
UITableViewCell *emailCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] //email
UITableViewCell *passwordCell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] //password
Then assuming both UITableViewCell have a property in their header exposed for the UILabel you can do:
emailCell.myLabel.text //Value of the text in the cell
If email and password has allocated then You can directly access them in your IIBAction method like that
in your .h file delcare this to email and password UItextfield
#interface ViewController : UIViewController
{
UITextField *email;
UITextField *pass;
}
Now alloc and add it in your cell
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
email = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 140)];
}
// Table View Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (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(indexPath.row==0)
{
[cell addSubview:email];
}
if(indexPath.row==1)
{
[cell addSubview:pass];
}
return cell;
}
Now apply this IBAction on your login button
- (IBAction)loginBtnTouchUpInside:(id)sender
{
NSLog(#"email ==== %#",email.text);
NSLog(#"pass ==== %#",pass.text);
NSString *strEmail = email.text;
NSString *strPassword = pass.text;
}
I hope this is works for you
The easiest way is, save the textField value in a separate string in textField delegate like below.
-(void)textFieldDidEndEditing:(UITextField *)textField{
stringEmail = textField.text;
}
and use the string value for further process.Do it for both by setting tag.
Thank You.
-(void)ActivateLogin{
NSIndexPath *tmpIndexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *tmpIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cellCopy0 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath0];
CustomCell *cellCopy1 =(CustomCell*) [tableViewName cellForRowAtIndexPath:tmpIndexPath1];
NSString *strEmail = #""; NSString *strPassword = #"";
strEmail = cellCopy0.tf.text;
strPassword = cellCopy1.tf.text;
if(![strEmail isEqualToString:#""] && ![strPassword isEqualToString:#""]){
... //Do what you want to do with the data
}
else{
//Alert user to enter credentials
}
}
I want to hide a label of a cell in a tableview.
(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
}
}
This code is hiding the label in the last cell as I failed to specify the code to hide the swiped cell's label.
Help to specify the swiped cell label to hide.
displayLabel.hidden = TRUE;
I need a replacement for this code.
You can try below code its working perfectly on my side:
- (void)viewDidLoad
{
UISwipeGestureRecognizer *recog = [[UISwipeGestureRecognizer alloc]initWithTarget:self
action:#selector(handleSwipeRight:)];
recog.delegate = self;
[recog setDirection:UISwipeGestureRecognizerDirectionRight];
[testTable addGestureRecognizer:recog];
// add the swipe gesture recognizer to tableview;
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
}
- (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];
UILabel *aLabel = [[[UILabel alloc]init]autorelease];
aLabel.frame = CGRectMake(5, 0, 100, 40);
aLabel.text = [NSString stringWithFormat:#"aLabel %d",indexPath.row+1];
aLabel.tag = 1;//tag the labels
[cell.contentView addSubview:aLabel];
UILabel *bLabel = [[[UILabel alloc]init]autorelease];
bLabel.frame = CGRectMake(110, 0, 100, 40);
bLabel.text = [NSString stringWithFormat:#"bLabel %d",indexPath.row+1];
bLabel.tag = 2;//tag the label
[cell.contentView addSubview:bLabel];
UILabel *cLabel = [[[UILabel alloc]init]autorelease];
cLabel.frame = CGRectMake(215, 0, 100, 40);
cLabel.text = [NSString stringWithFormat:#"cLabel %d",indexPath.row+1];
cLabel.tag = 3;//tag the label
[cell.contentView addSubview:cLabel];
}
return cell;
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:testTable];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [testTable indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [testTable cellForRowAtIndexPath:indexPath];
for (id label in cell.contentView.subviews)
{
if ([label isMemberOfClass:[UILabel class]])
{
UILabel *referedLabel = (UILabel*)label;
if (referedLabel.tag == 2) //tag of bLabel;
{
referedLabel.hidden = YES;
}
}
}
}
}
I think displayLabel is pointing to label in last cell of your table. Where you are setting value to displayLabel.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
//You should get label from cell here. Currently displayLabel may be pointing to label in last cell, if I am correct you are assigning value to displaylabel in cellForRowAtIndexPath or in any other method.
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
Change your method to this:
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
// If you created a custom cell with a
// displayLabel property, change the pointer
// type from UITableViewCell to that type
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.displayLabel.hidden = YES;
// Use the following three lines instead, if your cell style is "Subtitle"
// cell.textLabel.hidden = YES;
// cell.imageView.hidden = YES;
// cell.detailTextLabel.hidden = YES;
[cell setNeedsDisplay];
}
}