Objective C: How to add "plus" button in cell - objective-c

Can anyone advise me on how I can add a "plus" button in a UITableView Cell like what you see in the screen shot below?

You can also put the table view into editing mode, implement editingStyleForRowAtIndexPath: and return UITableViewCellEditingStyleInsert (which puts a green circle with a plus sign).
For example...
- (IBAction)editButtonPressed
{
//toggle editing on/off...
[tableView setEditing:(!tableView.editing) animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return UITableViewCellEditingStyleInsert;
//gives green circle with +
else
return UITableViewCellEditingStyleDelete;
//or UITableViewCellEditingStyleNone
}
When the green button is pressed, the table view will call tableView:commitEditingStyle:forRowAtIndexPath::
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert)
{
//handle insert...
}
else
{
//handle delete...
}
}

The simple way: get an image of the plus, set it for the cell.imageView.image.

You can customize your cell by getting the contentView property of your UITableViewCell and adding the UIImage of the plus button as a subview.

That's not a built-in control. It's probably a custom-style UIButton with that image in its .image property.

Related

Cell text overlaps the editingAccessory when try to delete a row in iOS7

When I compile my app in the new iOS7, I found a problem when entering in the edit mode of an UITableView.
When I press in the red minus button to delete a row of the table, this row indent to the left to let appear the 'Delete' button. However, when this button appears, the text of the cell overlaps the editingAccesory (this happens only when the text is longer than the length of the cell).
How can I remove the overlapping?
Edit: Images in the comments
Edit 2: Tis is the code of the creation of the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tweetList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SessionDetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Tweet *tweet = [_tweetList objectAtIndex:indexPath.row];
cell.textLabel.text = tweet.text;
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
Tweet *deletedTweet = [_tweetList objectAtIndex:indexPath.row];
[_selectedSession removeTweetsObject:deletedTweet];
[deletedTweet deleteEntity];
_tweetList = [Tweet findAllSortedBy:#"index" ascending:YES withPredicate:[NSPredicate predicateWithFormat:#"session == %#",_selectedSession]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
[[NSManagedObjectContext defaultContext]saveToPersistentStoreWithCompletion:nil];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
selectedPath = indexPath;
[self performSegueWithIdentifier:#"EditTweet" sender:self];
}
Solution:
Finally, I put the accessoryButton in the default state, and I use the edit state only to delete the rows. It's the only solution I've found :(
Perhaps, the method "willTransitionToState" can help people to solve similar problems.
You can hide or remove editingAccesory in editing mode , so there is no overlapping there,
set this,
Screenshot:
I came across this question, because in iOS 8.3 I faced the same problem, that it seems not possible to correctly display the editing accessory AND the delete confirmation without the cell content and the accessory item to overlap. Solving this issue without breaking the transition-animations was quite a challenge. ;)
So here is my solution (assuming that you are using autolayout constraints in IB):
Create a UITableViewCell subclass and link it to your table view cell in IB.
Add an outlet from the autolayout constraint specifying the horizontal spacing bewteen the right-most view and the cell's content view (trailing to margin).
Override willTransitionToState and layoutSubviews as shown below.
Table cell subclass:
#IBOutlet weak var horizontalSpaceConstraint: NSLayoutConstraint!
override func willTransitionToState(state: UITableViewCellStateMask) {
if (state & UITableViewCellStateMask.ShowingDeleteConfirmationMask == UITableViewCellStateMask.ShowingDeleteConfirmationMask) {
self.horizontalSpaceConstraint.constant = 49.0; // ugly, delete-confirmation width
}
super.willTransitionToState(state)
}
override func layoutSubviews() {
if (!self.showingDeleteConfirmation) {
self.horizontalSpaceConstraint.constant = 0.0;
}
super.layoutSubviews()
}
The reason why I cannot use didTransitionToState() (and use layoutSubviewsinstead) to reset the layout constraint is, that this function is simply not invoked (as of iOS 8.3) after transitioning from the delete-confirmation-state. It seems Apple did only handle the case, that the user actually deletes the row, but not the case that the delete-confirmation is closed without deleting the row. :(

How to hide a section in a table view controller with static cells?

I have a UITableViewController with static cells, with 3 sections, and a segmented control with 2 buttons.
I would like to achieve the following behavior:
when button 1 is pressed hide section 2
when button 2 is pressed hied section 3
I cannot find a solution to this.
Any tip is useful.
Thanks.
Simple, just make sure you set your UITableViewDelegate, and you can use heightForRowAtIndexPath: (and similar for headers and footers) to show/hide the cells by setting their height to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
if (self.shouldShowSection2) {
return 44.0f;
}else{
return 0.0f;
}
}else if (indexPath.section == 3) {
if (self.shouldShowSection3) {
return 44.0f;
}else{
return 0.0f;
}
}else{
return 44.0f;
}
}
Then just define some logic within your IBAction to change these BOOLs around in between tableview's begin/end updates, and the table will show/hide the sections you want.
- (IBAction)toggleSegment:(UISegmentedControl *)sender
{
[self.tableView beginUpdates];
// change boolean conditions for what to show/hide
[self.tableView endUpdates];
}

Disable Swipe-to-Delete Gesture in UITableView

Is it possible to disable the 'swipe-to-delete' gesture for a row/cell in a tableView? If so, how do you do it? The cell should still be editable in EDIT mode, but the swipe-to-delete gesture should be disabled.
Here's what to do:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
if (self.tableView.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
You still need tableView:commitEditingStyle:forRowAtIndexPath: to animate the deletion.
This is a much cleaner solution than iBrad Apps' solution, since you can use the default self.editButtonItem instead of a custom button.
Link: UITableView disable swipe to delete, but still have delete in Edit mode?
Yes. The code below will disable it.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
Response to your comment:
Edit2: My code below will work better with a custom button. If you want the default button then go to the link that #dasblinkenlight posted.
So pretty much make a button where you want the edit buttons to show and then call this method.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableview setEditing:editing animated:animated];
}
If you don't want to allow certain cells to be swiped and deleted, here's a solution:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == yourCellIndex) {
return NO;
}
return YES;
}
Also don't forget to have this implemented in order for it to work:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// do something when deleted
}
}
Heres a Swift version:
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if tableView.editing {
return UITableViewCellEditingStyle.Delete
}
return UITableViewCellEditingStyle.None
}

UITableViewCell swiped but delete button doesn't appear

This is odd. I'm right swiping a UITableViewCell in the iPad simulator. Even though the event below fires and the swipedCell is not nil, the Delete button doesn't appear. Actually, it appears-but only sometimes. I never get a bad access or a sigbart.
Here's the code:
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
if (userListSwipeRightRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [userListSwipeRightRecognizer locationInView:self.outletView];
NSIndexPath *swipedIndexPath = [self.outletView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
[swipedCell setEditing:YES];
}
}
Is this just a simulator issue or am I doing something wrong?
If you simply want to enable swipe-to-delete on your table, there is a much easier way to do it. Implement tableView:commitEditingStyle:forRowAtIndexPath: in your data source and the table view will automatically show the delete button when a cell is swiped.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
If you defined your UITableView in your header, then try:
swipedCell = [self.outletView cellForRowAtIndexPath:swipedIndexPath];
If you use a custom cell and override setEditing, you must call the super method or your delete controls will not be drawn.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
}

how to make UITableViewCellEditingStyleInsert button call insertNewObject

I add a cell to the end of a section when in editing mode and I give it UITableViewCellEditingStyleInsert. how do I set what function that button calls?
Use this method of UITableView:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == editStyleRowInd){
//any actions here
}
}
Where editStyleRowInd index of last row in the table