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
Related
I need to keep my first cell always located at top of tableview when i move others cell.I spent a lot of time and many ways button i haven't figure out that how to solve this problem.
This is my code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...do something to custom first cell design from xib file
//...do some thing to custom normal cells(cells at below of first cell)
[firstcell setEditing:NO animated:YES];
firstcell.userInteractionEnabled=NO;
if (indexPath.row==0)
{
return firstcell;
}
else
{
return cell;
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) // Don't move the first row
return NO;
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// i just change datasource for tableview at here
}
And there is my tableview when I move cell (normal cell).
I wanna keep first cell (blue cell) always be at top and not be interact by others cell.
You need to implement one more delegate method:
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
if (proposedDestinationIndexPath.row == 0) {
// Don't allow a row to be moved to the first row position
return [NSIndexPath indexPathForRow:1 inSection:0];
} else {
return proposedDestinationIndexPath;
}
}
This code assume you only have one section in your table view.
The point of this method is to tell the table view that if the proposed destination for the row being moved isn't appropriate, the returned value should be used. As written here, any attempt to move a row to the top will result in it being moved just below the top row.
I use UITableView with prototype cells and would like the top most cell to be an "adder" cell: when user taps on that cell, the segue is being performed. As the UITableView is being dynamically generated (except the top one) and cells are being reused, I have two question:
1 - is it possible to make one static cell while all the others will remain prototype cells?
2 - If not, how to implement this solution? I think, it has to be implemented in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
but I have no idea, is this method right place to do it. Am I correct, to implement this custom cell, I need to check, whether indexPath.row = 0 and substitute the needed cell with custom, while programmatically shifting all the cells one row down?
Yiu can do on the Tap of Your cell, you can create two different cell, one your prototype cell and one for your details
in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
AdderCell *cell = (AdderCell*)[tableView dequeueReusableCellWithIdentifier:#"AdderCell" forIndexPath:indexPath];
} else {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:#"CustomCell" forIndexPath:indexPath];
}
}
on tap of your Adder cell in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// add data to your row array and reload the tableRow
} else {
// do your tap action
}
}
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
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];
}