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];
}
}
Related
How can I show the edit menu for each word that is in each cell in the UITableview?
The picture below is an example of what I want to do. I want to be able to select a word from the label viewed in each cell, and show the edit menu for that word.
You need to put some code inside tableview delegate.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//fetch the content of label ex: "cell.labelName.text"
//Then put it on text box and allow editing ex:
textBox1.text=cell.labelName.text
textBox1.userInteractionEnabled=YES;
}
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];
}
I changed the color of text for the cell clicked in the table. But after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?
This is the code in "didSelectRowAtIndexPath"
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
Thank you
after the cell is clicked, when i come back to table the text of cell has the original color. Could you give me an advice?
You need to have the color for each cell stored somewhere other than in the table, so that you can reproduce the colors you want anytime the table redraws itself. Typically, you'll have some sort of data structure that stores the table's data, and that's usually the right place to save any changes the user makes. The table view's data source should have a -tableView:cellForRowAtIndexPath: method that sets the color according to what you've saved, along with any other cell attributes.
This is happen because the cells are reused, so lets say when you change text colour property of some cell it will be affected as you expect but when you scroll and that cell disappear off the screen it will be put to reuse pool and if it appears again on the screen table view takes some cell from the reuse pool but it's properties will be different so the colour won't persist.
You should keep somewhere, for example in NSMutableArray, info about which table was clicked.
You can add an index path to the array when you click the cell and in cellForRowAtIndexPath: check is this indexPath in the array and if it is change appropriate property.
The problem is that iOS throws away your cell if you scroll away and recreates it when it's needed (you scroll back to the cell).
If I were you, I would subclass UITableViewCell and overwrite
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
In there you would have
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected: selected animated: animated];
self.textLabel.textColor = selected ? [UIColor blueColor] : [UIColor blackColor];
}
Since iOS UITableView remembers which cell is selected, this should work fine, even when it's recreated.
The reason it's happening is what others are saying: cells are reused.
Storing selection state or color will work, however if you just need to make sure that selected cells have a different color for a label than non-selected cells, there's a way that does not require to use a supporting data structure.
You just need to check if the cell being setup at - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is currently selected or not, and that can be achieved with [tableView indexPathForSelectedRow] if your table uses single selection, or [tableView indexPathsForSelectedRows] if it uses multiple selection.
The last case requires you to find the current indexPath in the returned array, and might be slower than using the supporting array.
But if the selection is simple, then this solution is probably faster, uses less memory and is easier to read (IMO).
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.
I have a tableview called allone.h with cells that are all subtitle style cells with a disclosure indicator.
The subtitle text is generated dynamically, so it's pretty likely each cell will have different subtitle text.
I have an if statement that says if the text = "hello, world", then change accessory type to detailDisclosureIndicator. When the detailDisclosureIndicator is pressed, it pushes a new controller, changes the subtitle text, then pops that view controller and returns to allone.h. The problem is, it still shows the detailDisclosureIndicator even if the text != "hello, world". I tried changing the style in viewWillAppear, but I don't have access to cell.
Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//populate cells with stuff
if([cell.detailTextLabel.text isEqualToString:#"hello, world"]){
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
}
So I guess my question is, how do I change the accessory type back once I return to allone.h and the cell's text is no longer "hello, world"?
You can do that in your -[UITableView cellForRowAtIndexPath:]. Just like an "else" for your if statement.
And to reload the cell, you can just call reloadData or reloadRowsAtIndexPaths:withRowAnimation:.
I think cell.detailTextLable is equal to nil. So cell accessory doesn't change. Please double check it. did you configure the cell before?