The else part of this works fine. But if itemFlag is true, I can't get the new information to display itself correctly after doing the alphabetic sort. Quitting the programme and restarting shows the new item to have been added successfully to the array, and everything is diplayed correctly.
So my question is, why is reloadData not working if itemFlag is true?
Here is the code:
- (void)changeFoodDetails:(NSString *)foodName withPoints:(NSString *)foodPoints{
if (newItemFlag) {
[_foodStuffsArray insertObject:[[FoodItem alloc] initWithName:foodName points:[foodPoints intValue]] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[sharedManager sortFoodStuffArrayByName];
[self.tableView reloadData];
newItemFlag = false;
}else {
[_foodStuffsArray removeObjectAtIndex:currentFoodstuffArrayEntry];
[_foodStuffsArray insertObject:[[FoodItem alloc] initWithName:foodName points:[foodPoints intValue]] atIndex:currentFoodstuffArrayEntry];
[self.tableView reloadData];
}
}
Related
Whenever I click on my table view row to insert a large row below it (370px) it freezes, but only if the row I am clicking on is near the bottom of the screen. The CPU usage goes up to 100% and memory continues to increase but no error.
I have already viewed this question and this didn't help UITableView insertRowsAtIndexPaths:WithRowAnimation without freeze UI
here is my code -- self.sectBool basically tracks whether or not we have inserted the row
else if(indexPath.row == 0 && indexPath.section == 4){
self.sectBool = !self.sectBool;
NSMutableArray *indexPaths = [NSMutableArray array];
[indexPaths addObject:[NSIndexPath indexPathForRow:1 inSection:4]];
if (!self.sectBool) {
dispatch_async(dispatch_get_main_queue(), ^{
//[self.tableview reloadData];
[self.tableview beginUpdates];
[self.tableview insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:4]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableview endUpdates];
//[self.tableview reloadData];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
//[self.tableview reloadData];
[self.tableview beginUpdates];
[self.tableview deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:4]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableview endUpdates];
//[self.tableview reloadData];
});
}
}
any help is much appreciated!
I can's seem to be able to remove all table rows. What seems to happen is that only the cells that are in view are removed and then the ones that were out of view move up. Also checked and my data source contains only the cels that are in view at the time of deletion. Heres the code
NSMutableArray *left = [[NSMutableArray alloc]init];
NSMutableArray *right = [[NSMutableArray alloc]init];
for(int i=0;i<dataSource.count;i++){
[dataSource removeObjectAtIndex:i];
NSIndexPath *ip = [NSIndexPath indexPathForRow:i inSection:0];
if(i%2==0)
[left addObject:ip];
else
[right addObject:ip];
}
[_view.tableView beginUpdates];
[_view.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:left]
withRowAnimation:UITableViewRowAnimationLeft];
[_view.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:right]
withRowAnimation:UITableViewRowAnimationRight];
[_view.tableView endUpdates];
What happens now is that only the cells in view are removed and the rest pop in after the removal (also the issue seems to pop up since the dataSource only contains the rows that are in view for some reason). What i want is to remove all the rows. Any ideas?
The problem is in the for loop condition. The dataSource.count gets smaller each iteration. Try this:
for(int i=0;i<dataSource.count;i++){
NSIndexPath *ip = [NSIndexPath indexPathForRow:i inSection:0];
if(i%2==0)
[left addObject:ip];
else
[right addObject:ip];
}
[dataSource removeAllObjects];
try something like this:
[_view.tableView beginUpdates];
UITableViewRowAnimation animation;
for(int i = dataSource.count - 1; i>= 0; i--)
{
[dataSource removeObjectAtIndex: i];
NSIndexPath* ip = [NSIndexPath indexPathForRow: i
inSection: 0];
if(i % 2 == 0)
{
animation = UITableViewRowAnimationLeft;
}
else
{
animation = UITableViewRowAnimationRight;
}
[_view.tableView deleteRowsAtIndexPaths: #[ip]]
withRowAnimation: animation];
}
[_view.tableView endUpdates];
You can do this in easy way...
[dataSource removeAllObjects];
[_view.tableView reloadData];
i think that you can put your dataSource = #[]; and [self.tableview reloadData]; and remove all cell in your tableview
i have a menu that contain 6 cells 1st one contain UIViewController the second contain UITableView
when i click the first cell a new page appear,and it contain a button
when i click this button i want access to the second cell from the Menu, that contain a tableView
// use this code for to Refresh or delete or insert TableviewCell
// reloading cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// inserting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// deleting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationBottom];
As much i understand your question you want to move from a tableview to other view on click..So,you have to use code similar to this..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Detail *img1=[[Detail alloc]initWithNibName:#"Detail" bundle:Nil];
img1.labelString = [titleArray objectAtIndex:indexPath.row];
[self presentViewController:img1 animated:YES completion:nil];
}
Write following coed in your UIButton Tapped Method.
-(void) ButtonTapped:(UIButton *) sender
{
NSIndexPath *indexpathNew = [self.tblView indexPathForCell:(UITableViewCell *)[sender superview]];
NSLog(#"%#",indexpathNew.row);
}
indexpathNew is return indextPath of selected button of cell.
Try my code i might be helpful to you.
The matter is, I want to have a grouped table view, and when I click on a cell, I want to have an another cell to appear under it, and so on. Everything is fine with workflow, but I have same problems with animations. It works very slowly and I can see all changes
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"selected section:%d selected row:%d",indexPath.section,indexPath.row);
NSIndexPath* pathToDelete;
[_tableView beginUpdates];
if (_selectedPath != nil && [_selectedPath isEqual:indexPath]){
//If the same row was selected
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row+1 inSection:_selectedPath.section];
_selectedPath = nil;
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
} else {
//If not
if (_selectedPath != indexPath){
//delete the row that we selected last time
if (_selectedPath != nil){
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row inSection:_selectedPath.section];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
//add new row in the section we selected
_selectedPath = indexPath;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
}
[_tableView beginUpdates];
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
[_tableView endUpdates];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:pathToDelete.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:_selectedPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView endUpdates];
}
I think the problem is with begin & endUpdates method, but I don't know where exactly. Any clue?
Thanks
You could do this much simpler, by using a single beginUpdates - endUpdates. You just make your deletions and insertions inside of it. Don`t forget to update the data source for the table, otherwise the app will crash.
Hope this helps!
I am creating an iOS app that is set up as a hierarchy of data. I have no issues on the first page adding and deleting objects or even transitioning to the next page. The problem occurs on the second page which is set up exactly like the first. When I press the add button to add an object the program crashes and sends back the error SIGABRT.
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; << crashes on this line
here is the function add tapped:
- (void)addTapped:(id)sender {
StudentDoc *newDoc = [[[StudentDoc alloc] initWithTitle:#"New Student" rating:0 thumbImage:nil fullImage:nil] autorelease];
[_students addObject:newDoc];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_students.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
number of rows in section:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _students.count;
}
I have been under the impression that the count is what is causing the problem, but I have monitored the count and it stays consistent.
Overall I have no idea why it is crashing at this point because the page before it uses the exact same function to add objects on that page. Any ideas?
Normally if you'd messed up your array or row indexes you'd expect an exception to be logged in the console, not a SIGABRT. Have you enabled NSZombies and break-on-exceptions in Xcode? That may help to diagnose the issue.
You should put insert row at index path between begin updates and end updates block.
- (void)addTapped:(id)sender {
StudentDoc *newDoc = [[[StudentDoc alloc] initWithTitle:#"New Student" rating:0 thumbImage:nil fullImage:nil] autorelease];
[_students addObject:newDoc];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_students.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];
[self.tableView endUpdates];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
I've done almost the same, tho I split it into 2 lines:
NSInteger idx = [_students count] - 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
I wonder if that would make a difference.
You're adding an object to the data source, but replacing an existing row in the table, not adding one, resulting in one row in the indexPath but 2 items in the data source array.
For instance, if you have one item in _students, you'll have one index path, {0,0}. When you add a row, you're adding it at indexPath {_students.count - 1, 0}, which would be {1 - 1, 0} or {0, 0}. Your data source should match any indexPaths you insert or remove, and in your case you're always ending up with one more item in your data source than you add/remove.
If you always want to add an item, instead of:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_students.count-1 inSection:0];
You want:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_students.count inSection:0];