Custom UITableViewCell delete button not showing - uitableview

I created a custom UITableViewCell subclass with multiple fields and some other properties. Since I used this new cell in my UITableViewController, the cells do not react on the "swipe to delete" gesture.
To test if I'm missing something in my delegate I added a plain standard UITableView to my xib, hooked up dataSource and delegate and returned a regular UITableViewCell from -tableView:cellForRowAtIndexPath: this enabled the gesture again.
I searched on the net and one suggested to plain implement -layoutSubviews which didn't work too.
Is there something I would have to implement in my subclass?

After hours of searching and not finding anything on the internet, I did a whole day trial and error to find out that in my XIB the Editing was set to Multiple Selection During Editing.
I didn't know that this would disable the "Swipe-To-Delete-Functionallity".
As I need both possibilities (s2d unless editing and multiple selection while editing) I added these two lines in my -toggleEditing: method:
if(![_tableView isEditing]) {
[_tableView setAllowsMultipleSelectionDuringEditing:YES]; // <----
[_tableView setAllowdSelectionDuringEditing:YES]; // <----
[_tableView setEditing:YES animated:YES];
}
else {
[_tableView setAllowsMultipleSelectionDuringEditing:NO]; // <----
[_tableView setAllowdSelectionDuringEditing:NO]; // <----
[_tableView setEditing:NO animated:YES];
}

Related

How to add Storyboard headerview to tableview created at initialization by UITableViewController

I have a subclass of UITableViewController.
By default, it creates a UITableView when it is initialized. To that tableview I have set a header that I created in Interface Builder in the screen that is controlled by the controller. The header has two buttons:
one to enter editing mode for the tableview (called "Edit")
one to add a random item to the tableview (called "New").
I linked an IBOutlet property called headerView to the header from Interface Builder and I set it to be the header of the UITableView created at initialization in the viewDidLoad method.
The problem is that when I press the "New" button (which adds a new row with a new item to the tableview) the header of the tableview falls down to the bottom of the tableview.
Any idea why? How can I make it stick to the top?
This is the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"UITableViewCell"];
[self.tableView setTableHeaderView:self.headerView];
}
This is the method that gets executed when the "New" button is pressed:
- (IBAction)addNewItem:(id)sender {
Item *newItem = [[ItemStore sharedStore] createItem];
NSInteger lastRow = [[[ItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
Thanks.
Possibly you've set up the layout for table header view in a wrong way. Was there any layout constraint on it?
By the way, this project on GitHub is a very simple example to demonstrate how simply it is to achieve what you want in the storyboard. It will produce the result like below:
Please use this project to compare with your current configuration.
I hope this helps in one way or another.

TableView doesn't reload in the textViewShouldEndEditing method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am making an app which uses tableView having TextView as a Cell in it. And it adds more cell on click of footer of the tableView.
Now i want to reload table when textView done the editing.For that i put the [tblView reloadData] in textViewShouldEndEditing but at this time table doesn't reload.
Please help me.
You need to verify that the textView.delegate is set to an instance of the class that implements textViewShouldEndEditing. If the TextView is in a UITableViewCell subclass, then you might be setting that subclass as the delegate for the UITextView, when in fact you've implemented the delegate methods on the view controller that contains the tableView.
One approach would be this:
Put your UITextView in a UITableViewCell subclass.
Implement the UITextViewDelegate methods in your view controller that contains the UITableView. (Which may, in itself, be a UITableViewController, but it could just be a UIViewController that has a UITableView in it; either one is fine.)
Make a relationship between the cell and the containing view controller using code similar to this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
MyCustomCellSubclass *cell = [tableView dequeueReusableCellWithIdentifier....];
cell.textView.delegate = self;
// do further cell customization if needed
return cell;
}
Then when your cell's textView finishes, it'll call back to the containing view controller.
Also, from your description, it sounds like you might prefer the delegate method:
- (void)textViewDidEndEditing:(UITextView *)textView;
instead of "shouldEndEditing", but for what you're describing, that isn't your current problem.
As an added tip, if you do [tblView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewAnimationRowAutomatic], it'll refresh the tableview using a nice little animation, instead of a hard reload like reloadData does.
First Make sure you have UITextView Delegate for the self View. Like
UITextView.delegate = self ;
After that in
- (BOOL)textFieldShouldReturn:(UITextView *)theTextField {
if (theTextField == TextView) {
[Tableview Reload];
}
return YES;
}
Try this , Let me know.
Make sure your UITextView Delegate Methods are calling by putting NSLog try with using following Delegate Method of UITextView.
-(void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
[self performSelector:#selector(reloadTableViewData) withObject:nil afterDelay:0.3];
}
- (void)reloadTableViewData {
[self.tableView reloadData];
}

Add rows in table View with switch control like reminders app

In the Apple reminders App, and in the details screen of a remind,
when you switch on the control "Remind me at a location", a row "Location" is added (in fact a table view cell).
I would like to do the same in one of my application, when a switch control is actived 2 cells are added... how can i do this?
Thank you for your help
Check out this Project
https://github.com/singhson/Expandable-Collapsable-TableView
It may helps you.
in the method handling the change of your UISwitch you could either just reload the complete tableview using -(void)reloadData or (much nicer) use - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
You can use UISwitch as accessory view in one of your cells. However, you can face problems with reusing cells. If you need only one row with switch, you can simply add the UISwitch as a strong property in your TableViewController. You have to create it when initializing the controller:
self.locationSwitch = [[UISwitch alloc] init];
[self.locationSwitch addTarget:self action:#selector(handleSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
And then you can use it as an accessory view for your UITableViewCell:
cell.accessoryView = self.locationSwitch;
And you have to add (or remove) rows when switch value changes":
-(void)handleSwitchValueChanged:(id)sender
{
NSIndexPath* indexPath = // calculate your index path;
if(self.locationSwitch.on) {
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
And also remember to update your data source, so the value returned by tableView:numberOfRowsInSection is consistent with value of self.locationSwitch.

Use an IBAction to select and press a table cell row

I am trying desperately to make this IBAction just effectively press a cell at a selected row. I have managed to get it to select a row, but I can't work out how to effectively click on this cell! I am only making my first app but I have managed to figure most things out by myself, but just can't seem to find out how to do this, i'm hoping it is a simple solution (or there is a much better way to do it than I have).
Here is the code for my IBAction anyway:
- (IBAction)myButton:(id)sender {
// Specify which cell I wan't to select
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0];
// Select it
[self.tableView selectRowAtIndexPath:myIP animated:NO scrollPosition:UITableViewScrollPositionTop];
// Click this cell??
}
Thanks in advance for any help
Just tell the delegate that you've selected it
[self tableView:self.tableView didSelectRowAtIndexPath:myIP];
Assuming that self is your VC that controls the table.
The below stackoverflow answer looks like exactly what you need...
Automatically cell selected UITableView
Not a clean way to achieve but as per my understanding, You can add custom UIButton (transparent) on each cell such a way it covers almost complete cell in cellForRowAtIndexPath:, disable row selection. On these button you can use addTarget:action:
If your view controller that has the UITableView in it, is not subclassing UITableViewController you need to create an IBOutlet of the UITableView call it myTableView or whatever you'd like, then in your IBAction you can reference it like this:
- (IBAction)myButton:(id)sender {
// Specify which cell I wan't to select
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:0];
// Select it
[self.myTableView selectRowAtIndexPath:myIP animated:NO scrollPosition:UITableViewScrollPositionTop];
// (Per Dmitry's answer)
[self.myTableView didSelectRowAtIndexPath:myIP];
}

How to load new content into a UITableView using IBAction?

FYI - Noob iOS developer here.
My current setup is a UIViewController with a UIView within, then a UITableView within the UIVIew. So it goes like this...
UIViewController --> UIView --> UITableView
The reason for this is because I have other elements wrapped with the tableview. The UIViewController loads dynamic content into the table view. I have a segmented Control in which I want to use to switch the content within the table view.
I've read something on [table reload] and [table beginUpdate] but don't understand how to use it. Any help would be great.
You need to implement a method for UIControlEventValueChanged event ofUISegmentedControl for this.
[yourSegmentedControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
And implement the segmentChanged method like:
- (void)segmentChanged:(id)sender
{
UISegmentedControl *mySegment = (UISegmentedControl *)sender;
switch ([mySegment selectedSegmentIndex])
{
case 1:
//load first contents
break;
case 2:
//load second contents
break;
default:
break;
}
[self.yourTableView reloadData];
}
Ok, so [table reloadData] will reload the data (so if you change the data and want to update the table with the necessary data call this), but straight after you call that make sure to call [table setNeedsDisplay] to refresh the UI.[table beginUpdates]
begins a series of method calls that insert, delete, or select rows and sections of the receiver. You end the processes with [table endUpdates];
Make sure you set your table view's dataSource and delegate to self, this can be done through the xib and programmatically like this:
[table setDelegate: self];
or
[table setDataSource: self];
As said:
Call this delegate method for UISegmentedControl
- (void)segmentedControl:(UISegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)selectedIndex
{
if(selectedIndex == 0)
{
// Update the data
}
else if(selectedIndex == 1)
{
// Update the data
}
[table reloadData];
[table setNeedsDisplay];
}
For example,
Your UIView named *myView and your UITableView named *myTableView,
the time you want to reload tableview, in your UIViewController , you should reload tableview like this:
[self.myView.myTableView reload];
and make sure tableview's delegate and dataSourceDelegate is set correctly.
Need to set the delegate & data source for tableview in ViewController.h file like
UITableViewDataSource, UITableViewDelegate.
Implement delegate & datasource methods in ViewController.m file
[tableview SetDelagate:self];
[tableview SetDatasource:self];
Implement the delegate methods.
And reload the table using
[tableView reloadData];
method.

Resources