How can I repopulate a tableview depending on which UIButton is pressed? - ios

I am writing an image gallery application that has multiple categories in which the user can view photos. On one of the menus, there are three UIButtons that list the categories of photos: 1970s, 1980s, 1990s.
When the user taps on "1970s", it segues into a Table View where there is an array of photos and labels. Each Table cell has its own photo and corresponding label dictating the date and location of the photo. When the user taps on a cell, it leads to a UIImage view and a scroll view where the image can be viewed fullscreen and zoomed/panned. There is also a little popover view on the UIImage view that can be displayed via a button.
The problem I'm having is that the Table view, Image view, and popover view take up three Storyboard "controllers". I want to replicate this table view, fullscreen image view, and popover view across many different categories (UIButtons) I have all over the app.
Is there a way to repopulate a Table View depending on which UIButton is pressed?
Thanks for the help! I am in Swift 3 and using Xcode.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
- (void) 1970Action{
dataArray = 1970ImagesArray;
[self.tableView reloadData];
}
- (void) 1980Action{
dataArray = 1980ImagesArray;
[self.tableView reloadData];
}
- (void) 1990Action{
dataArray = 1990ImagesArray;
[self.tableView reloadData];
}

Related

Why should I tap twice on tableview cell to navigate to another view in iOS, objective C

In my app there is a table view with custom cell.in that cell there are (image view, views and labels).data loads succuessfully to the table.
Then what I wanted to display another view(detail view) when user tap on the cell.
I did it by (ctrl+drag) cell and connect it with the detail(second) view.
and selected the segue kind as Show.so then,when I click on a cell first time it correcly and and then I have to tap twice to load the detail view. I tried with changing kind to show Detail, present Modally, etc.but the no successful result. Then I tried with using navigation controller within these views.but didn't get any successful result.no idea what to do.I added some code and if you guys want more code I can provide.hope your help.thanx
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FlightRoundtripDetails *setDetails = [RoundItineraryArray objectAtIndex:indexPath.row];
resultCell.firstdepatairiportCodeLabel.text = setDetails.FirstDepartureAirport;
resultCell.firstarriairportCodeLabel.text = setDetails.FirstArrivalAirport;
resultCell.firststopoverLabel.text = setDetails.Outbound;
}
data display correcly.only the issue is when tap a cell.hope your help.thanx.
I think u have set the tableview selection as Multiple. So goto Attributes Inspector and change the selection to Single.

Scroll to selected cell in UITableView after selecting cell in UISearchDisplayController?

I want to set up a UISearchDisplayController such that after a cell is selected from the search results, the search display controller is dismissed and the table view scrolls to the selected cell. Here's my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
[self.searchDisplayController setActive:NO];
// scroll to selected cell in table view here
}
}
I know the method I need to use is:
scrollToRowAtIndexPath:atScrollPosition:animated:
However, this method needs an index path from the main table view, not the search results table view. How can I get this index path?
What does your datasource look like? Perhaps you can query the datasource based on what was selected in the search results to get the cell's index.
Sounds like a strange user interaction not knowing what you're doing though. Normally searching and then selecting will take you to that record's details or select it as an option or something.

How to Implement Tab View in Apple Maps

I want to implement the Tab View like Apple has in their Apple Maps when you select a location and tap on the more details to reviews additional information about that location.
What would be the best way to implement this or how do you think Apple has implemented it. I know each tab view has a Table View inside each. Also for the Photos tab how do you think they implemented the content inside that tab. Was it using UICollectionView or a table view with a custom cell that has 4 photos in each cell row?
It looks like they're using one UITableView and on UISegmentedControl. With this combination every time the user selects and index on the segmented control, it changes a condition in the table's datasource and reloads the data. Here's an example of what something like that could look like to conditionally change the number of rows in the table
- (IBAction)segmentValueDidChange:(UISegmentedControl *)sender
{
NSLog(#"%d",sender.selectedSegmentIndex);
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.segment.selectedSegmentIndex == 0) {
return 5;
}
return 10;
}

Set UIButton title to selected table cell

I have a UITableView loading a different array when clicking on different buttons. What I want is to update the UIButton titles to the selected table cell. So for example: the user has selected table row 4 which has the text "April 2013". The button updates its title to "April 2013". This is working. Now, the user switches between TableView by clicking on a button (loading new data in the SAME array). The UITableView with table row 4 named "April 2013" updates its fields using the same array and the field previously selected is still preserved but now with different data. Yet the UIButton title is still showing "April 2013", I want to change it so that when clicking on the button and changing the data of the array, the button title changes aswell.
DidSelectRowAtIndexPath:
if([self.delegate respondsToSelector:#selector(dateSpecifiedButtonText:)])
{
[self.delegate dateSpecifiedButtonText:selCell.textLabel.text];
}
ViewController.m:
- (void)dateSpecifiedButtonText:strText2
{
[self.dateSpecifiedButton setTitle:strText2 forState:UIControlStateNormal];
}
In short: How do I update a UIButton title to show a current selected table cell.
I hope you understand the question.
Instead of setting the button title based on the contents of the cell, try setting the title based on the contents of the array:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleToUse = [arrayWithData objectAtIndex:indexPath.row];
if([self.delegate respondsToSelector:#selector(dateSpecifiedButtonText:)])
{
[self.delegate dateSpecifiedButtonText:titleToUse];
}
}

Why is my UILabel in a UIView in a UITableCellView not updating after a correct database call, (it seems cell reuse related)?

I have a UITableview cell that gets a tally from a core data database. The tallyTable is in a view controller inside a UITab view. I have an NSLog statement that prints out the tally value whenever it gets updated. Another tab has a list to change the source (different day) for the tallies. I am using iOS5 with ARC targeting iOS 4.2.
Here's the problem. When I load the application, the correct tallies for whatever the last selected day show up in the table tab. If I then go to the day tab and change the day and return to the tally tab there is no change in the display. However, the viewWillAppear on the tally tab runs and as the table cycles through cellForIndexPath, my NSLog statement prints out all the correct new values. If I then scroll the top label off the screen and back the label updates to the new value.
I've tried setNeedsLayout and setNeedsDisplay on the UILabel, the UITableViewCell, the UITableView and the view controller loading the table. I tried changing the CellReuse identifier so that it would never reuse a cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CollectionItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CollectionItemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.collectionKeys objectAtIndex:row] valueForKey:#"collectionTitle"];
NSInteger test1 = indexPath.row + 150;
NSLog(#"tag = %i", test1);
cell.tallyButton.tag = test1;
NSNumber * questionID = [[self.collectionKeys objectAtIndex:row] valueForKey:#"answerID"];
cell.tallyLabel.text = [NSString stringWithFormat:#"%i",[self updatePointTotal:questionID]];
NSLog(#"Collection text should be = %#", [NSString stringWithFormat:#"%i",[self updatePointTotal:questionID]]);
[cell setNeedsLayout];
return cell;
}
I've read over a half dozen other similar questions. Got about three hours invested so far in trying to solve this.
EDIT: I thought I fixed it by using the navigation controller to repush the top level view controller on to the view again. I'll admit now this feels like a classically kludgy hack in every way. When the view is PUSHED everything updates and it is seamless. However, in order to have a fixed footer to make selection settings for the table buttons, I used a UIView with two subviews, a UITableView on top and a simple UIView with four buttons below.
The captions on the buttons need to change with the data source. Now when the view controller is pushed onto the view it obscures my fixed footer view. So, I inserted the fixed footer into the UITableview and everything appeared fine until I scrolled the UITableView and the footer scrolled up with it. The table is basically a tally sheet with buttons next to each item and in the footer is four buttons to note the color of the tallied item. Say the next item was a green lego, you would tap "green" in the footer and the button next to "lego" in the table. When I push the view controller with the two subviews the UITableview labels do not update. Thus the tableview needs to be pushed itself (as far as I can tell).
ANSWER: see comment below but ultimately I needed to reload both the visible UITableView data and the delegate UITableView controller data behind it.
I'll give it a shot. First, are you using ARC? If not, you need to add autorelease when you alloc/init a new cell. Otherwise, it's fine as is.
If I'm understanding your question correctly:
The tableView displays the correct data at app launch
You switch away from the tab with the tableView and change the tableView dataSource
You switch back to the tab with the tableView and you see (via NSLog) that the table cells are reloaded with the correct data yet the old data is still visible in the cells
If you scroll a cell off the display and back forcing it to refresh it contains the correct data
Some thoughts:
the tableView will not reload itself automatically when it's view appears. You need to call [tableView reloadData] whenever the dataSource changes. This is independent of whether the tableView is currently displayed or not. My guess is this alone will solve your problem.
You don't need to call setNeedsLayout on the cell unless you want the cell to relayout its subviews based on the data. You also don't need setNeedsDisplay.
I'm assuming there aren't other complicating factors (such as multiple tableViews displaying the same data) that could confuse things.
If you use prepare for reuse method, remember to over the original method with [super prepareForReuse];
Another method if the above way does not work is re setup cell as describe here.
I use the same method i applied for some of my collection view : we should remove/reset your subview where you create/add it to cell 's content. That mean we need set up data each cell completely for each row.
I move the code reset data value from prepare reuse to where i set value and I worked simply !
In my CustomCell.m :
- (void)configCellWith:(id)item At:(NSUInteger)row {
if (_scrollView) {
[[_scrollView subviews]
makeObjectsPerformSelector:#selector(removeFromSuperview)];
_scrollView = nil;
[_scrollView removeFromSuperview];
}
else {
CGFloat y = labelHeight+15;
float scrollHeight = _imgArray.count*200;
_scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, y,SCREEN_WIDTH-20, scrollHeight)];
_scrollView.scrollEnabled=YES;
_scrollView.userInteractionEnabled=YES;
_scrollView.layer.masksToBounds = YES;
[self.contentView addSubview:_scrollView]; } }
Remember to change your data source appropriately too.

Resources