I'm using UISwitch on UITableViewCell. When I the push save button, I want to get UISwitch value. How can I get it? My code listed below:
- (void)save:(id)sender
{
UITableViewCell *commentCell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomInfoCell" forIndexPath: [NSIndexPath indexPathForRow:0 inSection:0]];
UISwitch* commentSwitch = (UISwitch*)[commentCell viewWithTag:2];
NSLog(#"%#", [NSNumber numberWithBool:commentSwitch.on]);
}
- (void)save:(id)sender
{
UITableViewCell *commentCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UISwitch* commentSwitch = (UISwitch*)[commentCell viewWithTag:2];
NSLog(#"%#", [NSNumber numberWithBool:commentSwitch.on]);
}
Related
-(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.
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 working with a UICollectionView and am having a bit of difficulty replicating something that I have working in a UITableView. I am looking to set the selectedBackgroundView of a selected UICollectionView cell and have that selection maintained every time the view is loaded.
I have a basic application with two tabs in a Tab Bar controller where both tabs are UITableViewControllers. The second tab is the In-App Settings and I have two cells in there; one for App Themes and one for Keyboard themes. The keyboard themes is another Table View but the App Themes is a UICollectionView with a grid of 3x4 cells. Although this is dynamically created, there's not going to be any more cells and this is it.
Working
Right now, I can select a UICollectionViewCell and happily apply a custom image on the top; that works very well.
Problem
The problem I am facing is the fact that the selected cell does not maintain that custom image on the top when the view reloads even though the value is saved in NSUserDefaults.
Because I have the same concept working with my Keyboard Themes (UITableView), I have copied the principles here, but am not sure if what I have done is actually correct.
Here's some code:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
ThemeCell *themeCell = (ThemeCell *)[self.cView cellForItemAtIndexPath:indexPath];
self.selectedThemeString = themeCell.cellLabel.text;
if(self.checkedIndexPath)
{
UICollectionViewCell *uncheckCell = [self.cView cellForItemAtIndexPath:self.checkedIndexPath];
uncheckCell.selectedBackgroundView = nil;
}
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
self.checkedIndexPath = indexPath;
UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
dot.image=[UIImage imageNamed:#"check-white-hi.png"];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
[[NSUserDefaults standardUserDefaults] setObject:self.selectedThemeString forKey:#"Selection"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
In the cellForItemAtIndexPath, I have:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ThemeCell *themeCell = (ThemeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"Theme Cell" forIndexPath:indexPath];
NSString *cellData = [self.themeLabels objectAtIndex:indexPath.row];
themeCell.cellLabel.text = cellData;
themeCell.cellImages.image = self.themeImages[indexPath.row];
UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
dot.image=[UIImage imageNamed:#"check-white-hi.png"];
self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:#"Selection"];
NSLog(#"Selected Theme String = %#", self.selectedThemeString);
if (!self.selectedThemeString)
{
NSLog(#"111");
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
if ([self.selectedThemeString isEqualToString:#"Original"])
{
NSLog(#"222");
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
if ([self.selectedThemeString isEqualToString:#"Mystical"])
{
NSLog(#"333");
self.checkedIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
themeCell.selectedBackgroundView = dot;
[themeCell addSubview:dot];
}
return themeCell;
My viewWillAppear has:
self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:#"Selection"];
if ([self.selectedThemeString isEqualToString:#"Original"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Peacock"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Mystical"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Zebra"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Simplicity"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Rainbow"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:5 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Prosperity"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:6 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Leopard"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:7 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Hypnotic"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:8 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Dunes"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:9 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Twirl"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:10 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:#"Oceanic"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:11 inSection:0];
}
[self.cView reloadData];
The order of the cells are defined above, so the first row first cell is called Original. The second cell first row is called Peacock and the third cell first row is called Mystical, etc.
My thinking was, I could define the indexPathForRow for each string and in the cellForItemAtIndexPath, when the string hits, it would apply the selectedBackgroundView to the image.
Status
What's happening now is, I can successfully select a cell and the image appears; if I reload the view (by going out and coming back in), the selectedBackgroundView from the cell doesn't exist. However, I know NSUserDefaults is working because in my cellForItemAtIndexPath, the "111", or the "222" or "333" is getting logged successfully, but it's just not setting the highlight state of the selected UICollectionCell.
What I require is to:
1) Have a selected cell display the selectedBackgroundView image when the view has been reloaded.
Update:
In the cellForItemAtIndexPath, if I put themeCell.backgroundView = dot instead of themeCell.selectedBackgroundView = dot, when I reload the view, every single cell highlights with the custom image instead of just the selected cell, so I think I'm making progress somewhat, but still lost.
Any guidance would be really appreciated.
Situation averted.
I have fixed this issue.
For reference, I will put the fixed code here for anyone experiencing the same issue.
In the cellForItemAtIndexPath, I did the following:
if (!self.selectedThemeString)
{
NSLog(#"111");
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
themeCell.backgroundView = dot;
[themeCell addSubview:dot];
}
if([self.checkedIndexPath isEqual:indexPath])
{
NSLog(#"Loaded");
themeCell.backgroundView = dot;
[themeCell addSubview:dot];
}
else
{
NSLog(#"Not Loaded");
themeCell.backgroundView = nil;
}
This is similar to the code earlier, but essentially copied from my UITableView code which checks the indexPath.
The other change is the fact that I used the backgroundView property rather than selectedBackgroundView.
I did this change throughout my class, so wherever I had the selectedBackgroundView, I changed that to backgroundView.
I'm happy and this works like a treat.
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
}
}