pop up a view when i clicked button on table view cell - objective-c

I have a button on table view cell if I click the button a pop up view will be appeared and the remaining cells will be animated to downwards like animation.and the cell having sub views it will open like a plist in ios if i click the sub view cell it will open the next level sub cells. The tableview must be like below images
Cells and sub-cells must be like this. Can any one assist me?

I have done this before. Just use the following code. Create a custom cell class having the cell and followed view in the cell view but show only what is to be shown by using heightforrowatindexpath method uitableview class. Here is the code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if (checkHeight == indexPath.row)
return 185;
else
return 80;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
FAQCell *cell = (FAQCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"FAQCell" owner:self options:nil];
cell = myCell;
myCell = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.quesText setFont:[UIFont fontWithName:#"CaviarDreams" size:16]];
[cell.ansText setFont:[UIFont fontWithName:#"CaviarDreams" size:16]];
// check for the row for which the background color has to be changed
if (checkHeight == indexPath.row)
[cell.bgView setBackgroundColor:[UIColor colorWithRed:0.3333 green:0.7373 blue:0.4588 alpha:1.0]];
else
[cell.bgView setBackgroundColor:[UIColor colorWithRed:0.9176 green:0.3765 blue:0.2980 alpha:1.0]];
return cell;
}
#pragma mark -
#pragma mark Tableview Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
checkHeight = indexPath.row;
// reload the data of the table
[tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, tableView.numberOfSections)] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Let me know if you face any issue.

Check out this custom view -
Custom Tree View
Expandable Table View

Related

How to hide the textLabel property of the default UITableViewCell from all UITableView rows?

I am playing with some UITableView coding and tried to hide the default textLabel property. I used the UIScrollViewDelegate protocol and used both -scrollViewDidScroll and scrollViewDidEndDecelerating methods to hide and show the label.
The code works fine just with the first row of the table and not all of them which is what I want. Here's my code:
*Edited to show the solution code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.hidden = NO;
cell.textLabel.text = #"TEST";
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (cell in [self.tableView visibleCells]) {
cell.textLabel.hidden = YES;
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
for (cell in [self.tableView visibleCells]) {
cell.textLabel.hidden = NO;
}
}
So anyone could help me to hide and show all the rows?
It looks like you've tried declaring your cell as a variable in your table view controller or as a global somewhere. That's not going to work because it will always be set to whatever the last cell dequeued was. You should definitely declare that as locally in tableView:cellForRowAtIndexPath:.
Hiding the textLabel in scollViewDidScroll and showing it again in scrollViewDidEndDecelerating should work fine, you just have to make sure you're hiding/showing all the currently visible cells in the table. Luckily, there's a method of tableView that'll help with that: visibleCells. That returns an NSArray of UITableViewCells which you can loop over and hide the textLabel.
So, it should look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
cell.backgroundColor = [UIColor redColor];
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (UITableViewCell *cell in [self.tableView visibleCells]) {
cell.textLabel.hidden = YES;
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
for (UITableViewCell *cell in [self.tableView visibleCells]) {
cell.textLabel.hidden = NO;
}
}
Try putting this cell.textLabel.hidden = YES;in your cellForRowAtIndexPath method. that is where each cell is created.
NOTE: This is just one possible solution, There might be other, more optimized solutions out there.
Get the visible cells from your table each time the table begins scrolling with scrollViewDidScroll:. Then iterate over each cell and set it's textLabel hidden.
Get the visible cells in scrollViewDidEndDecelerating: and set the textLabel visible.
Get the visible cells with yourTableView.visibleCells.

How to show in a UILabel what you Selected in a UITableViewCell (didSelectedRowAtIndexPath)

I am coding a registration form where users must enter their country. I listed the countries through a .plist file in a UITableView (Classe ViewController). In addition , I set the single selection with the checkmark. After selected the country the APP must go back and show in the country label the selected row (like a normal reg form).
Here whats under didSelectRowAtIndePath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Uncheck the previous checked row
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
Thanks for helping me.
Richard
If you have your label and your table in the same controller you can do this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.yourLabel setText:[self.arrayCountries objectAtIndex:indexPath.row]];
}
but if you have your table and your label in differents controllers you can use NSUserDefaults for keep your selection like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[NSUserDefaults standardUserDefaults]setObject:[self.arrayCountries objectAtIndex:indexPath.row] forKey:#"selectedCountry"];
}
and in your controller where you have your label get this selection
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.labelCountry setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"selectedCountry"]];
}
As I understood, you should use delegates. Once the country selected, call a delegate function in previous form and set the value of cell text.
e.g., (untested code to give an idea)
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
// Trigger previous form to get the current form's data
// Here self.delegate takes address of previous form
[self.delegate setThisCountryNameToOwnerFormWithText:cell.titleLabel.text];
// Probably go back like this
// [self.navigationController popViewControllerAnimated:YES];
}
Just add that line like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
country.text=uncheckCell.label.text;//add this wherever you want, but it should be in this method only
}
where country is the label you created.
Also this line of code will work only if your label that is country and your table where you select the row both are in the same class,could you mention how you have made your classes like where your UITableView is?and where your UILabel is?

Hide the circle on UITableView with multiple selection

I have a UITableView that hast multiple selection enabled.
Some of my cells are not to be selected. For those I implement:
in the tableView:cellForRowAtIndexPath:
I set cell.selectionStyle = UITableViewCellSelectionStyleNone;
and in tableView:didSelectRowAtIndexPath:
I call [self.tableView deselectRowAtIndexPath:indexPath animated:NO];.
This works fine. My only caveat is that the little circle left of the cell still appears. It does not get checked when a user taps the cell, but I would like for it not to be shown, when a cell is "unselectable".
How can I hide those circles for certain cells?
Thanks
You have to implement the tableView:canEditRowAtIndexPath: method in your table view datasource. It will let you prevent the cells you want to be editable and thus the circles not to be shown.
Beware that setting cell.selectionStyle = UITableViewCellSelectionStyleNone; does NOT prevent the cell to be selected. It just removes any visual clue on a selected cell.
Try this...
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Subclassing UITableViewCell and adding following method works for me:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(false, animated: true);
}

Default CellAccessory

How can I set first row of tableview checkmarked when the app started? I mean when I start app now, there are few rows in tableview and when I click on some of them its accessory change to checkmark. When click another one, the another one comes checkmarked and previous checkmark dissapears. So now I want first row of tableview to be checkmarked when app is started and then when I click another row it 'uncheckmark' and 'checkmark' new row etc...
Try the options suggested in these posts how to apply check mark on table view in iphone using objective c? or iPhone :UITableView CellAccessory Checkmark
Basically you need to keep track of the checkmarks using say a dictionary or so. And In viewDidLoad or init method make the first cell as checked in the dictionary. While drawing cells, always check if the corresponding entry in the dictionary is checked or not and display check mark accordingly. When user taps on a cell, modify the value in dictionary to checked/unchecked.
Update:
Here is a sample code.
In .h file declare a property as
#property(nonatomic) NSInteger selectedRow;
Then use the below code,
- (void)viewDidLoad {
//some code...
self.selectedRow = 0; //if nothing should be selected for the first time, then make it as -1
//create table and other things
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// create cell and other things here
if (indexPath.row == self.selectedRow)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// some other code..
if (indexPath.row != self.selectedRow) {
self.selectedRow = indexPath.row;
}
[tableView reloadData];
}
You could set an integer to a variable similar to "checkedCell", have that value default to cell 0, and in the cellForRowAtIndexPath check to see if the cell is already checked, if not change the accessory to make it checked and update your variable.
-(void)viewWillAppear{
currentCheckedCell = 0;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
// Create cells -- if indexpath.row is equal to current checked cell reload the table with the correct acessories.
//
static NSString *cellIdentifier = #"RootMenuItemCell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
for (UIView *view in nibContents) {
if ([view isMemberOfClass:[MyCell class]]) {
cell = (MyCell *)view;
break;
}
}
//OTHER CELL CREATION STUFF HERE
// cell accessory
UIImage *accessory = [UIImage imageNamed:#"menu-cell-accessory-default.png"];
UIImage *highlighted = [UIImage imageNamed:#"menu-cell-accessory-selected.png"];
if(indexPath.row == currentCheckedCell){
//DEFAULT ACCESSORY CHECK
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:highlighted];
}else{
//DEFAULT ACCESSORY NONE
// cell.accessoryType = UITableViewCellAccessoryNone;
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessory highlightedImage:accessory];
}
[cell setAccessoryView:accessoryView];
}
return cell;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//UPDATE SELECTED CELL & RELOAD TABLE
currentCheckedCell = indexPath.row;
[self.tableview reloadData];
}
Also worth noting that my examples uses custom images for accessories.
If you check out the link #ACB provided you'll find a very similar concept.
You can use,
if (firstCellSelected)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Set the firstCellSelected flag in viewDidLoad method.

How to generate a second table view when the user touches a cell in the first (dynamic) table view (in Objective-C)?

I have a table view with dynamic cells generated in the class TableViewController. I made a segue to another table view (which inherits from TableViewController2). I want to trigger the display of the new table view when the user selects a cell from the first table view. I implemented prepareForSegue in TableViewController. The segue works, I can see the first table view pop off screen and the new table view pop in, but it's empty whatever I do. How can I trigger the display of the cells in TableViewController2?
In TableViewController:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([(segue.identifier) isEqualToString:#"topSegue"]){
TableViewController2 *navigationController = segue.destinationViewController;
TableViewController2 *tvc = segue.destinationViewController;
}
}
In TableViewController2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Top 50 list";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
return cell;
}
Did you define the following two methods in your second tvc?
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView