uitableview doesn't prints row index value for every clicks - ios

Im using the following code to get the index of tablecell that got clicked.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView setAllowsSelection:YES];
NSLog(#"you have selected %d",indexPath.row);
}
The problem with this code is that it prints the index value of row only once. If I tap the tableviews anymore, it doesn't print the index value. How to know which index I have clicked ?

try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSLog(#"you have selected %d",indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView setAllowsSelection:YES];
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell * tableCell = [tableView cellForRowAtIndexPath:indexPath];
BOOL isSelected = (tableCell.accessoryType == UITableViewCellAccessoryCheckmark);
if (isSelected)
{
tableCell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Now check when accessory check marks appears it will print the index path or not??

Related

How the grab the value of textview from a UITableView on didSelectRowAtIndexPath?

When a table view is pressed, I am trying to grab the value of a textview from that table view row that has been selected as follows.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyOrdersController *detailVC = [[MyOrdersController alloc]init];
NSLog(#"hello %#", detailVC.shipmentReferenceNumberTextLabel.text);
}
But im getting null value. How can I get the value of the textview in that specific row?
You can grab the textview from selected cell subview.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
for (id txtView in selectedCell.subviews)
{
if ([txtView isKindOfClass:[UITextView class]])
{
UITextView* tv = (UITextView*)txtView;
NSString* text = tv.text;
}
}
}
Just try it:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
}

Detail view not updating when UITableView rows are re-ordered

I have a UITableView that pushes to a Detail ViewController. In my tableview, my rows are draggable/re-arrangeable. However, when the rows are switched, the path stays the same when selecting the rows and the Detail View is presented. Example: TableView Re-order Pic
If I were to switch the "Milk" row with the "Gym" row, then select "Gym", the detail view would still return the details of the milk row.
How do I fix this so that the path of the rows switch appropriately as they do?
.m :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self->newArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *theFood = [values objectAtIndex:indexPath.row];
cell.textLabel.text = [theFood valueForKey:#"name"];
cell.detailTextLabel.text = [theFood valueForKey:#"price"];
return cell;
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[_myTableView setEditing:NO animated:NO];
// [_myTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[_myTableView setEditing:YES animated:YES];
// [_myTableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if( fromIndexPath == toIndexPath ) {
return;
}
NSDictionary *moveFood = [self->newArray objectAtIndex:fromIndexPath.row];
[self->newArray removeObjectAtIndex:fromIndexPath.row];
[self->newArray insertObject:moveFood atIndex:toIndexPath.row];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detailSegue"]) {
NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
DetailViewController *detailVC = segue.destinationViewController;
detailVC.detailFood = [values objectAtIndex:indexPath.row];
}
}
When you switch the rows you should also update your data source. Otherwise when tableView's didSelect method is called it would access the wrong object in your data source array.
So in –tableView:moveRowAtIndexPath:toIndexPath: switch the location of the objects that were switched.
Special thanks to #HotLicks for helping me!
The problem was I didn't change my datasource for the detail view from values NSArray to my newArray NSMutableArray after the update in my tableView:moveRowAtIndexPath:toIndexPath: method!

Checkmark in a tableview, only one selected

Hi I know how to add a tick mark on each row when you select it, but how can you only allow ONE to be ticked ?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.CityTableView deselectRowAtIndexPath:indexPath animated:YES];
[self.CityTableView setTintColor:[UIColor grayColor]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.CityTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
this does it for every one, but I want it to remove the last one and add the new one when another one is pressed.
thanks
Create an instance variable as NSIndexPath *selectedIndex;
In your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Your cell Initialization here
cell.accessoryType = [indexPath isEqual:selectedIndex] ?UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath * prevIndexPath = selectedIndex;
selectedIndex = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevIndexPath, indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}

removing a UITableViewCell on a tap

In my UIViewController, I have a table view whose cells show text. The datasource and delegate of the UITableView is my UIViewController. The text to be showed by the cells is stored in an NSMutableArray.
I am able to populate the cells with the text initially, but I also want to implement a functionality, when a user clicks on a specific row the cell gets removed and the table is reloaded.
Here's my code and it's details:
taskList is a NSMutableArray
png images are ones that i need to add in every cell
my table contains only one section
the last row of the table will always show "Create New Task" with a "+" sign as an image
as user adds other task (handled by a separate controller), the taask gets added to the current table.The task will have a "-" sign image.
When user clicks on such a row, it will get deleted from the table
UIViewController's code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
taskList = [[NSMutableArray alloc] init];
addNewTask = [[AddTaskViewController alloc] init];
[taskList addObject:#"Create New Task"];
[taskList addObject:#"aaaaa"];
[taskList addObject:#"bbbbb"];
[taskList addObject:#"ccccc"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
indexNumberInTaskList = [taskList count] - 1;
return [taskList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
}
if(indexNumberInTaskList >= 0)
{
cell.textLabel.text = [taskList objectAtIndex:indexNumberInTaskList];
if(indexPath.row == ([taskList count] - 1))
{
cell.imageView.image = [UIImage imageNamed:#"add.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:#"remove.png"];
}
indexNumberInTaskList = indexNumberInTaskList - 1;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int selectedRow = indexPath.row;
if(selectedRow == ([taskList count]-1))
{
[self presentViewController:addNewTask animated:YES completion:NULL];
}
else
{
[taskList removeObjectAtIndex:indexPath.row];
[self.createDaily reloadData];
}
}
The least amount of code way would be:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.modelArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
These are only required to batch multiple changes together:
[tableView beginUpdates];
[tableView endUpdates];
You can use didSelectRowAtindexPath method of tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow: indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourArr removeObjectAtIndex:indexPath.row];
[YourTbl reloadData];
}
Under your -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method,
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
NSArray *toBeDeleted = [[NSArray alloc] initWithObjects: indexPath];
// Data deletion
[tableData removeObjectAtIndex:(indexPath.row)];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:toBeDeleted withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
Edit: seems everybody has the same answer!
Add this method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow: indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
}

Iphone tableview

I am using below code for getting index number of the row. I want to get the Text of row. Please tell me, what modifications are required to get the text from each row.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[self.navigationController pushViewController:detail animated:YES];
detail.rowNum.text = [NSString stringWithFormat:#"Row: %d", (indexPath.row) ];
}
thanx in advance :)
You can get cell with all its properties this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
detail.rowNum.text = [NSString stringWithFormat:#"Row: %d", (indexPath.row) ];
detail.someLabel.text = cell.textLabel.text;
[self.navigationController pushViewController:detail animated:YES];
}
Is this what you want? Text from selected cell?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[self.navigationController pushViewController:detail animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
detail.rowNum.text = cellText;
}
Update ur code as above.

Resources