Tableview won't reload data after selecting row - objective-c

When I select any row in tableView data should be removed. I have set selected text in textField which is working, but tableView data is not cleared after selection, code given below.
Please help me.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[searchResult removeAllObjects];
[self.tblViewSearch reloadData];
}

If you really want to remove the table view you could try this. This is a bad idea if you have set up your table view with interface builder, since it does the creation for you in this case.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[self.tblViewSearch removeFromSuperview];
}
A better strategy might be to just hide it, and then show it again when needed. To make it look nicer you could add an animation too.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[UIView animateWithDuration:0.3 animations:^{
self.tblViewSearch.alpha = 0.0;
}];
}

Related

TableViewCell push to another ViewController very laggy

I embedded the navigation controller to the UITableViewController, and for every cell I need click the cell to push to another viewcontroller. The problem is when I enable the animation, it would be very laggy clicking the cell. I don't know why.
The code is following as:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = #"test";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SliderViewController *vc = [[SliderViewController alloc] init];
[self.navigationController pushViewController:vc animated:NO];
}
I think it is quite normal so I don't know what's the problem.

Swipe to delete

I Have a tableview and a cell in it :D with disclosure accessory selected. I created all ui elements with storyboard.
(didn't put a view in content of the cell)
I'm using SWTableViewCell to implement just swipe-to-delete, but everything seems work fine except when i put a breakpoint on to a method
#pragma mark -SWTableViewDelegate-
-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
NSIndexPath *path=[self.table indexPathForCell:cell];
[anArray removeObjectAtIndex:path.row];
[self.table deleteRowsAtIndexPaths:#[path] withRowAnimation:UITableViewRowAnimationRight];
}
and this will help you to understand what i simply done here
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SWTableViewCell *cell=(SWTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (!cell)
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f
green:0.231f
blue:0.188
alpha:1.0f]
title:#"Delete"];
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell" containingTableView:_table leftUtilityButtons:nil rightUtilityButtons:rightUtilityButtons];
}
NSMutableDictionary *deDict=[[NSMutableDictionary alloc]initWithDictionary:[anArray objectAtIndex:indexPath.row]];
cell.textLabel.text= [deDict objectForKey:#"Name"];
return cell;
}
Yet, I don't get any error, because when i try to swipe on simulator, it simply does not work..
No need to use SWTableViewCell just Override following built-in methods of UITableView delegate for support conditional editing & swipe to delete
for conditional editing -- Optional --
//Override this method only if you are going to be returning NO for some items.
//By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return YES if you want the specified item to be editable.
return YES;
}
for swipe to delete
// Override to support editing the table view.
// for swipe to delete
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}

Custom UITableViewCell in static UITableView

I've defined a custom UITableViewCell that I'm using in my static UITableView. I'd like to access the variables I've defined (myTableViewImageCell, etc) but doing it from tableView:willDisplayCell seems to say something along the lines of "redefinition of cell type.
Here's what I'm doing:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
}
If all of your cells are the same custom class, you can just change the definition of your method to indicate that and get rid of your first line (which is wrong anyway):
- (void)tableView:(UITableView *)tableView willDisplayCell:(ANMyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[cell.myTableViewLabel setTextColor:[UIColor hex666Color]];
}
Note however, that if you are doing the same thing to all of your cells (as you show here), you should do this in cellForRowAtIndexPath: instead of willDisplayCell:. That way, the code is run only once when the cell is created instead of every time that it is displayed.
FYI, the problem is the extraneous ;cell; you have at the end of the second line. change it to
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
ANMyTableViewCell *cell = (ANMyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[cell.myTableViewLabel setTextColor:[UIColor hex666Color]];
}

Different ViewController called when TableView is Editing

I have a simple grouped table view that when a cell is selected it pushes a View controller. The navigation bar's left item puts the table view in "editing mode." In editing mode you can move and delete each cell as wanted. What I want is to be able to push a separate View controller when it is editing mode then when it is in normal mode.
This is where the View Controller is being pushed:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...stuff that doesnt matter...
[[self navigationController] pushViewController:detailViewController animated:YES];
}
The BarButtonItems are being declared here:
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
This is where all the editing stuff is declared:
- (void)tableView:(UITableView *)atableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[atableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)atableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[BNRItemStore sharedStore] moveItemAtIndex:[sourceIndexPath row]
toIndex:[destinationIndexPath row]];
}
When you push your view controller in the didSelectRowAtIndexPath, you can check if the tableview is in editing mode. If it is, you can push a different view controller.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...stuff that doesnt matter...
if(aTableView.editing) // The tableview is in editing mode
[[self navigationController] pushViewController:otherViewController animated:YES];
else [[self navigationController] pushViewController:detailViewController animated:YES];
}
editing is a property that returns YES if the tableview is currently in editing mode.

Activate cell's accessory view (UISwitch) when didSelectRowAtIndexPath is called

Every cell of my tableView has a UISwitch as an accessoryView:
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = mySwitch;
[mySwitch addTarget:self action:#selector(SwitchToggle:) forControlEvents:UIControlEventValueChanged];
Switch is toggled when the user taps it, but I also want it toggled when the didSelectRowAtIndexPath is called (when the user taps on that cell).
I tried this (but it doesn't work):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *tempSwitch = (UISwitch *)[tableView cellForRowAtIndexPath:indexPath].accessoryView;
[tempSwitch addTarget:self action:#selector(SwitchToggle:) forControlEvents:UIControlEventValueChanged];
[tableView deselectRowAtIndexPath:indexPath animated:YES]; // cell selection fadeout animation
}
Thanks.
The code must look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *tempSwitch = (UISwitch *)[tableView cellForRowAtIndexPath:indexPath].accessoryView;
[tempSwitch setOn:!tempSwitch.on animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Remember that you also have to update your model.