Delete UITableView cell - xcode

I am creating a small ipad application using the UISplitViewController which has a UIMasterViewController and a UIDetailViewController. I have removed the UITableView the comes with the UIMasterViewController and I have created my own UITableView by dragging and dropping one from the Xcode Objects panel. I have maned to populate the UITableView with data successfully however when i try to delete a cell or record from it I seem to get an "Thread 1: signal SIGABRT" error.
Below is my edit button code. (Which is a custom button):
//Edit Button
int cnt = 0;
- (IBAction)buttonEditPressed:(id)sender {
if (cnt == 0){
[self.myTableView setEditing:YES animated:YES];
_buttonEdit.title = #"Done";
cnt++;
}
else if (cnt == 1){
[self.myTableView setEditing:NO animated:YES];
_buttonEdit.title = #"Edit";
cnt--;
}
}
And the following code is where the deleting should happen (But gives me the erros instead):
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
[self.moduleTitleStack removeObjectAtIndex:indexPath.row];
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.myTableView reloadData];
}
[UPDATE] numberOfRowsInSection implementation:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
//return [sectionInfo numberOfObjects];
return [_numberOfRows count];
}
I would think that this would do the job but not sure why I am getting this error.

For the answer:
You don't need to do [tableView reloadData]; if you just deleted the row and nothing else changed.
Also, the crash you are getting, is that you are deleting an object from your self.moduleTitleStack, but the row count is checking for _numberOfRows. (I'm guessing they are different arrays), and that's the problem you are getting. You need to update the _numberOfRows to reflect the new setup of your table view
A general tip to know more about the exceptions you are getting:
Go to the breakpoints panel on the left, and on the lower left, click Add > Exception Breakpoint and then just hit OK. The debugger will stop whenever an exception is thrown, giving you some info about the exception, as well as the exact stack trace of where and how it happened

Related

Dynamic sections with dynamic cells with Core Data?

I am really close to the solution, but i cannot figure out the rest, so i am asking for help now...
I use core data, so I'd like to show, whats inside of it.
Here is my code so far:
at .m
#property NSMutableArray *users, *sharedWishes;
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
self.context=delegate.managedObjectContext;
self.users=delegate.collectUserNames; //collect every users from database
self.sharedWishes=[delegate collectSharedWishes]; //collect every wish from DB - each user has different number of records
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// works great so far
return self.users.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
User *user=[self.users objectAtIndex:section]; //shows the username for each section
if(self.users.count<1)
return #"no friends";
else
return user.userName; //this is just fine, too
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SharedWishes *wish=[self.sharedWishes objectAtIndex:indexPath.row]; //this is where the problem is...the tableview shows the records until the other user records starts
static NSString *identifier=#"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
...
}
So, any suggestion for the cellForRowAtIndexPath:, what should I add?
Every section should have a title (which has) for the current user name, and every section should contain that particular person's records (which fails). Instead, when new section begins (with new user) the existing records starting to repeat. How should I avoid it?
Thanks in advance!
EDIT: added this few line to cellForRowAtIndexPath:
User *user=[self.users objectAtIndex:indexPath.section];
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
self.currentUserWishes=[delegate collectCurrentUserSharedWishes:user];
Now it works!

UICollectionView reloadItemsAtIndexPaths

I've been trying to wrap my head around the reloadItemsAtIndexPaths method of UICollectionView.
I have an array of objects called objectsArray. When a user scrolls to the bottom of my collection view, I fetch the next batch of objects from the backend and append it to objectsArray by calling [objectsArray addObjectsFromArray:objects]; After doing so, I call [self.collectionView reloadData] which I know is expensive.
I'd like to optimize with the code below but I get an assertion failure when calling reloadItemsAtIndexPaths.
if (self.searchPage == 0) {
parseObjectsArray = [[NSMutableArray alloc] initWithArray:relevantDeals];
[self.collectionView reloadData];
} else {
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (int i = 0; i < [relevantDeals count]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[indexPaths addObject:indexPath];
}
[parseObjectsArray addObjectsFromArray:relevantDeals];
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
//[self.collectionView reloadData];
}
Error:
* Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:3716
Any help/tips is greatly appreciated.
Thanks!
It looks like the items being added are new, in other words you are adding items which weren't there before. In that case, just replace reloadItemsAtIndexPaths:indexPaths with insertItemsAtIndexPaths:
[self.collectionView insertItemsAtIndexPaths:indexPaths]; // Use this for newly added rows
//[self.collectionView reloadItemsAtIndexPaths:indexPaths]; // Use this for existing rows which have changed
The assertion error was misleading. It wasn't until after I implemented
#try
{
[self.collectionView insertItemsAtIndexPaths:indexPaths];
}
#catch (NSException *except)
{
NSLog(#"DEBUG: failure to insertItemsAtIndexPaths. %#", except.description);
}
Did I realize was calculating my indexPaths wrong. My index offset for the new objects was off by one making my collectionview think I was adding more objects than I specified in numberOfItemsInSection.
Thanks for all those who attempted to help!
This error often arises when you increase/decrease the number of items in the array but it doesn't match the data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Are you updating it appropriately?

tableView cell - select from another tableView

I am wondering if and how it could be possible to get the following working
From a tableView cell I want to get to another tableView with i.e. 10 different cells where the user has to choose on, and this one is set up in the previous table cell.
So let's say i.e
The table cell is "choose" - touching this cell a tableView with 10 different cells opens up and the user can choose one of them. By choosing one of them it gets back to the inital tableView and the cell is filled with the users choice. How exactly to achieve this?
Hello you can check with below code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(tableView == displayTableView)
{
//Implementation for choiceTableview
choiceTableview.hidden = FALSE;
displayTableView.hidden = TRUE;
[choiceTableview reloadData];
}
else
{
//Implementation for displayTableView
choiceTableview.hidden = TRUE;
displayTableView.hidden = FALSE;
[displayTableView reloadData];
}
}
You have to check below condition in tableview method and display data according to ti:
if(tableView == displayTableView)
{
//Implementation for choiceTableview
}
else
{
//Implementation for displayTableView
}
This will help you to handle as many table view as you want. Cheers...

How to present specific view controller when a cell is selected on iOS 6?

I am using Xcode 4.5.2, and iOS6.
I have 3 View Controllers, FirstViewController contains dynamic table view.
What I want is, when the first row is selected it takes the user to SecondViewController, and when the second row is selected to the ThirdViewController.
Below is the didSelectRowAtIndexPath method of my code
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *path = indexPath;
NSInteger theInteger = path.row;
switch (theInteger)
{
case 0:
//here i wanna present FirstViewController
break;
case 1:
//here i wanna present SecondViewController
break;
default:
break;
}
}
I am not sure if this is the best practice but this is all what I know. I hope I made it clear.

Adding rows to a table

I have a table with objects added with NSArray called listOfConjProcedures I want to use an insert control that appears above the top row in the table to add rows to my table when tapping the edit button in a UInavigation controller and I cannot find a good sample code.
The edit function looks like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEditing])
return [listOfConjProcedures count] + 1;
else
return [listOfConjProcedures count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[listOfConjProcedures removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
I don't know how to proceed with the insert function to introduce a new row when the edit button is tapped (At the bottom of the posted code).
Thank you in advance.
This links helps you for insert new row link1 link2 link3

Resources