Is it possible to highlight multiple UITableViewCells? Something like a filter table. If the user taps on row 0, it gets highlighted. Then user taps on row 1 and that also gets highlighted but row 0 stays highlighted also. I haven't seen any examples for this.
I have a dictionary with 5 items which can get selected, values of which are either yes or no to keep track which ones are highlighted.
I used (UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath and set it to nil but now I can't unselect rows. Any help please?
What you can do to implement this is keep a track of the selected indexes and change the backgroundcolor of table viewCell accordingly, for example:
Declare an Array as SelectedIndexes
Then in your didSelectRowAtIndex method just do this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if([selectedIndex containObject :indexPath]){
[selectedIndexes removeObject:indexPath];
}else{
[selectedIndexes addObject:indexPath];
}
[tableView reloadData];
}
And then In your CellForRowAtIndexPath Method do this
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([selectedIndex containObject :indexPath]){
cell.backgroundColor=highlightedColor;
}else{
cell.backgrounfColor=NormalColor;
}
}
Hope this Helps
Related
I'm having a UITableView with the table view cell loaded from an .xib.
Which has a label text and a checkmark button.
I want to change the checkmark image of the button when the cell is selected. (Allows multiple selection).
From these selected cells with changed checkmark image, i'm performing an operation.
Can anyone tell me how this could be done?
Thanks in advance....
Follow the below to change the button image.
Single Selection
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (selectedIndex == indexPath.row) {
// change image here...
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath.row;
[tableView reloadData];
}
If you need for multiple selection then you can maintain a array to capture the indexes and do the needful like below.
Multiple selection
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([selectedIndexArray containsObject:indexPath.row]) {
// change image to check mark here...
} else {
// Change image to un-check mark
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selectedIndexArray containsObject:indexPath.row]) {
[selectedIndexArray removeObject:indexPath.row];
} else {
[selectedIndexArray addObject:indexPath.row];
}
[tableView reloadData];
}
You can have a field isCheckMarked in your model class, which you have then put in array for your tableview to populate. You can then toggle that flag in didSelectRow delegate of tableView and can change checkmark image based on isCheckMarked
I have gif animated pictures in some uitableview rows,
When there is a lot of gifs in tableview CPU usage is going to be very high,
So I want to hide these gifs when their row is not visible,
How can I do that ?
How can I achieve not visible cells row's indexPath ?
After that I can hide the gif like that :
UITableViewCell *celll = [ tableView cellForRowAtIndexPath:indexPath];
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = TRUE;
So I must get not visible cells indexPath's in a loop.
Generally, you don't need to do that as long as you properly use reusable cells.
Nevertheless, if you do want to do that, you can use tableView:didEndDisplayingCell:forRowAtIndexPath: method on UITableViewDelegate:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = YES;
}
If you'll put the line:
NSLog(#"%#",indexPath);
as the first line of the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You'll realise that it's done automatically. Only the visible cells, i.e the one that appears on the screen, are the one you present the data for.
Sorry for poor english.You had not posted code about how your cellForRowAtIndexPath implementation look like,i will suggest to make your cellForRowAtIndexPath like below.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"YOURIDENTIFIERSTRING";
UITableViewCell *cell = [tableView dequeReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
// Configure the cell here …
return cell;
}
This way only those many cells will be created which are visible on device and once row goes out of device view it will be reuse for new row. Please check apple doc for tableview.
I have a UITableViewController created in storyboard. It has two sections. The first section's rows contain controls laid-out in storyboard. I want to update the rows in the second section using values in an array.
I'm fairly new to iOS development. I understand how to use a UITableViewDataSource to update a table based on the array, but not how to restrict the updates to a specific section. Can anyone outline how to do this?
EDIT This seemed like a simple problem, so I thought I code would just obscure the question. Maybe I was wrong. Heres what I have:
My numberOfRowsInSection function returns 1 in the section number is 0, because the first section (the one I designed in storyboard) has a single row, otherwise it returns the number of elements in the backing data array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 1;
else
return [myData length];
}
My cellForRowAtIndexPath function creates a cell if the section number is 1. But I don't know what to do if the section number is zero. How do I avoid having to recreate the rows I laid-out in storyboard?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
cell.textLabel.text = [myData objectAtindex:indexPath.row];
}
else
{
// What to do here?
}
}
Well If you only have few static controls in the first section why won't you put these controls in a table header view instead? Thus you'll only have one section to worry about :)
In your method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathadd this
Create 2 differents UITableViewCells and reference them like this
if (indexPath.section == 1) {
NSString *CellIdentifier = #"DynamicCell";
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//You are drawing your second section so you can use your array as values
cell.property1...
cell.property2...
cell.property3...
return cell;
}else{//If you have only 2 sections then else represent your first section
//You are drawing your first section
NSString *CellIdentifier = #"StaticCell";
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
You can change the row value in the delegate method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
To identify the section, just use:
indexPath.section
You can use reloadRowsAtIndexPaths: with an array of all the indexPaths that are in the wanted section, built with a loop and a NSMutableArray.
- (void)reloadSections:(NSIndexSet *)sections
withRowAnimation:(UITableViewRowAnimation)animation;
The parameter "section" is An index set identifying the sections to reload.
I have a TableView with three sections. Section one and two can be deleted, (delete with swipe)
section three can't. My problem now is that when I swipe on a cell in the third section, it gets selected. I tried deselecting it when "canEditRowAtIndexPath" is called(and it really gets called), but that doesn't work and the cell remains selected. My "canEditRowAtIndexPath" looks like this:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section == 2)
{
return NO;
}
return YES;
}
I found ( in my opnion ) a better solution to this matter
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Right)
I hope that helps!!
Regards
You may check and close the 3rd section cell selections in -(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath by using tableViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
You should call [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; in this method -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Or in -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Set selection style none. cell.selectionStyle = UITableViewCellSelectionStyleNone;
Use this delegate method of UITableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I found a great workaround.
A swipe on the cell, forces the app to call viewWillAppear.
Add [self.tableView reloadData];into that method and the selected state will be removed.
I have a UITableView and I am trying to get the text of all the selected rows when a button is pressed. Here's what I have so far:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[selectedRows addObject:selectedCell.textLabel.text];
}
selectedRows is an array.
Here is my button press function where I need access to the checked row titles:
- (IBAction)selectList:(id)sender {
NSLog(#"%#", selectedRows);
}
This doesn't work for me because if a user unchecks a row and then clicks the submit button - it is not reflected in the array. I would appreciate some help with this. thanks.
Instead of dealing with keeping tabs on what is/isn't selected at any given moment, why not just wait until it's time to select the list to generate it and let the table keep track of what's selected. (provided it isn't some massive amount of data) You could loop through the index paths of your table's selected cells and pull the string you're looking for directly out of the datasource.
- (IBAction)selectList:(id)sender
{
NSMutableArray *arrayOfText = [NSMutableArray new];
for (NSIndexPath *idx in self.tableView.indexPathsForSelectedRows) {
[arrayOfText addObject:dataSource[idx.row]];
}
NSLog(#"%#",arrayOfText);
}
You could add a didDeselectRowAtIndexPath function and remove the item from the array.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[selectedRows removeObject:selectedCell.textLabel.text];
}