IBAction selection of row in TableView - ios

I have a TableView each row will open the same screen but with different locations for the views.
I want the images to open up on each view in a random way.
i have a random method which takes care of that but i am not sure how to call this method upon selection of a row in the TableView - This same method should be called for every row.
At this moment i am randomizing each view with a button - after the view loads i need to press the button and then the images are randomized.
Is there a way to call this method upon a selection of a row in the TableView?
-(IBAction) randomizeLocations:(UIButton*)sender {
for (MyView* myv in rndArry)
{
int x;
x = generateRandomNumber();
while (usedRnd[x] == 1)
{
x = generateRandomNumber();
}
myv.frame = rndArryloc[x];
usedRnd[x] = 1;
}
int i;
for (i=0; i<8; i++)
{
usedRnd[i] = 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
{
OneViewController* ovc = [[OneViewController alloc] initWithNibName:#"OneViewController" bundle:nil];
[self.navigationController pushViewController:ovc animated:YES];
// ???? WHAT SHOULD I PUT HERE ????
[ovc release];
}
break;
ect....

Thanks for the great and fast answers -
i have read again and again and finally figured out what i needed to do.
As Holger wrote u did call the method in the ViewDidLoad and everything is working now.
Thanks again - This Forum Rules :)

Related

How to close expanded table view cell programmatically?

I have two tabs in a view controller: on click of each tab, different data is displayed in a table view with cells that expand and close on click.
The problem is that when I'm in tab A, and expand a cell there, then go and click on tab B, loading new data, that cell is still expanded. How do I close it programatically?
I'm using the FZAccordionTableView subclass to perform the expansions.
- (UIView *)tableView:(FZAccordionTableView *)tableView viewForHeaderInSection:(NSInteger)section {
HeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kAccordionHeaderViewReuseIdentifier];
if (listOfCategoryWhichHaveItems.count > 0) {
if (arrCategory.count > 0) {
arrCategory1 = _btnProduct.selected == YES ? [arrCategory objectAtIndex:0] : [arrCategory objectAtIndex:1];
NSDictionary *dict = arrCategory1[section];
view.lblHeaderTitel.text = [dict valueForKey:kCategory];
bool isSelected = [tableView isSectionOpen:section];
}
}
return view;
}
#pragma mark - <FZAccordionTableViewDelegate> -
- (void)tableView:(FZAccordionTableView *)tableView willOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView *)header {
loadFirstTime = false;
[tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation: UITableViewRowAnimationAutomatic];
if (listOfCategoryWhichHaveItems.count > 0) {
_sectionNumber = &section;
NSDictionary *dict = listOfCategoryWhichHaveItems[section];
[self setDataAccordingToCategories:[dict valueForKey:kCategory]];
}
}
- (void)tableView:(FZAccordionTableView *)tableView didOpenSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView *)header {
}
Please set keepOneSectionOpen property of your table to No on clicking the tab B.
Yourtable.keepOneSectionOpen = No;
In the FZAccordionTableView GitHub repo i found this method:
- (void)closeSection:(NSInteger)section withHeaderView:(nullable FZAccordionTableViewHeaderView *)sectionHeaderView
That will probably do what you want, it is however not declared in the header file so you can't call it directly.
You can however clone the repo, add the line to the header file, add + commit + push to your fork. Then use that repo in your Podfile (or however you're using the library right now) so you're using your fork of the framework, then you can call the method.
On click of each tab, i used this
[_tblProductServies closeAllSectionsExcept:-1];
its working fine now, thanks for all.

How to show already expanded STCollapseTableView for all items?

I'm programmatically coding STCollapseTableView. What I want is when user enters the particular screen, all headers must be expanded by default without clicking any headers. And I don't want other header to be collapsed if one is expanded. Is it possible? If not then what is other way to achieve this?
I have headers and if some of the header items contain child items, it should have expanded already.
Edit: I have figured out that I need to call below method in order to expand headers without clicking.
- (void)handleTapGesture:(UITapGestureRecognizer*)tap
{
NSInteger index = tap.view.tag;
if (index >= 0)
{
[self toggleSection:(NSUInteger)index animated:YES];
}
}
How can I call this method after all headers are displayed?
I use below code to return cell as header views for my table. So I need to call handleTapGesture() method after reloading tblItems.
[self.headers removeAllObjects];
for (int i = 0 ; i < [arrItems count] ; i++)
{
[self.headers addObject:[self addViewintoCell:i]];
}
[tblItems reloadData];
[tblItems setContentOffset:CGPointZero animated:NO];
Here are one lazy solution, but it couldn't be used as standard solution, that you open all section in loading just after reloading table with your data.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView reloadData];
[self.tableView setExclusiveSections:NO];
//Get Section to open
NSInteger sectionCount = [self.tableView numberOfSections];
for (int needToOpen = 0; needToOpen<sectionCount; needToOpen++) {
//Open Section one by one
[self.tableView openSection:needToOpen animated:NO];
}
}

How Can I Change Tableview Array with Segment Control

I Have a one table view and i have two array. My arrays name AllItems and SpecialItems. I Use segment control. I wantto if segment value is 0 tableview load AllItems Array, When change segment value and value is = 1 than mytableview reload tada but SpecialItems array. Can u help me please. Thanks.
I solved this problem with table tag.
- (IBAction)segmentControlChanged:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 1) {
mytable.tag = 1;
}
else
{
mytable.tag = 0;
}
[mytable reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView.tag==1)
{
return [specialItems count];
}
else
return [allItems count];
}
You could create two data source classes that implement all the UITableViewDataSource methods: one for AllItems and one for SpecialItems. To switch between the two, connect a valueChanged action. In the method that is called, set the data source and reload the table view.
- (void)valueChange:(UISegmentedControl *)sender
{
if (/* condition for all items */) {
self.tableView.dataSource = self.allItemsDataSource;
} else {
self.tableView.dataSource = self.specialItemsDataSource;
}
[self.tableView reloadData];
}
I would personally create an array which the data is loaded from. Put this in your implementation:
NSArray * _tableData
Then in your viewDidLoad just allocate this for the array which we want it to start on.
_tableData = [[NSArray alloc] initWithArray:allItems];
This initially loads the data we will always see as the segment control starts on index 0. We have to set the initial data somewhere so the tableView loads with some data in it.
Then set the number of rows and the cellForRowAtIndex to pick up from the _tableData array
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bCell];
// Here we use the specific array as we would normally
return cell;
}
This step means the tableView will load with the array. Even if the array is empty the view will still load as the number of cells will be zero.
Now in our value changed function we can reset the array as we need to:
- (IBAction)segmentControlChanged:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 1) {
_tableData = allItems;
}
else {
_tableData = specialItems;
}
[self.tableView reloadData];
}
You just need to make sure the segment control changed is linked up in the XIB file (or programatically) and that you reload the table after choosing the array.
This kind of thing is actually really easy to do. I would definitely recommend working it through step by step if you're having trouble. Make sure each step is working before applying the next:
Get the tableView loading with both sets of data individually
Confirm that the segment control is calling the change function when clicked
Then that should do it

UITableView Back Button

I have two UItableViews and the problem i am having is in going back from the second UITableView to the first UITableView. I have applied the back button using:
- (IBAction)BackButtonClicked:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
The issue is that it takes me back to the previous UITableView but when I click on the row of that UITableView it returns an empty UITableView where there should be data in it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int i = 0;
if(indexPath.row == 0)
{
i = 0;
}
else
{
i = (indexPath.row * 4);
}
NSLog(#"%#", [ParsedData objectAtIndex:i]);
SubList *SubListNib = [[SubList alloc] initWithNibName:#"SubList" bundle:nil];
self->newSubList = SubListNib;
newSubList.folderid = [ParsedData objectAtIndex:i];
[self presentModalViewController:SubListNib animated:YES];
}
The Data gets entered in the Array the first time.
On going to the previous screen and coming back the data does not enter the array.
You should reload first tableview, when you returns from second tableview to first by
[firstTableViewName reloadData];
This method must also be include in backbutton click.

ViewDidLoad is not called for the second time Xcode

I have a tableViewController from which i navigate to UIViewController. To save the value of indexPath.row I have used an integer and based on that integer action is performed in UIViewController. Problem is that when I press the back button and selects another index of table, It navigates to UIViewContoller but the action performed is the first one. the ViewDidLoad is not called for the second time.
here is my code.
TableView Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.iaRecipieViewController) {
self.iaRecipieViewController = [[[iRecipieViewController alloc] initWithNibName:#"iRecipieViewController" bundle:nil] autorelease];
}
if (indexPath.row == 0) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
if (indexPath.row == 1) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
if (indexPath.row == 2) {
self.iaRecipieViewController.myLableArray =labelSoupArray ;
}
// custom string is an NSinteger
self.iaRecipieViewController.customString = indexPath.row;
NSLog(#" int %i",self.iaRecipieViewController.customString);
NSLog(#"index path %i ", indexPath.row) ;
[self.navigationController pushViewController:iaRecipieViewController animated:YES];
[detailTable reloadData];
}
UIViewController Code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myLabel.text = [self.myLableArray objectAtIndex:customString];
}
Use viewWillAppear:(BOOL)animated
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
myLabel.text = [self.myLableArray objectAtIndex:customString];
}
viewDidLoad will only get called when the view is loaded; pushing a new ViewController and then removing it won't force the view to be reloaded. viewWillAppear is called before the view is rendered, so you have an opportunity to do things whenever it becomes the primary view.
ViewDidLoad gets called only once when the view is instantiated. Based on your code, you are going back and forth on this view and as such the view is not instantiated (loaded) everytime but rather it was loaded once and now it is really the view disappearing and appearing as you go back and forward to that view.
Put your code in ViewWillAppear or ViewDidAppear.
The method
(void)viewDidLoad
is run when the view gets loaded.
If your view already loaded(means it's still in the memory),maybe it's gone from the screen. But it doesn't mean that the memory is gone; Just re-Run when the view should load to the memory.

Resources