NSOutlineView numberOfRows doesn't increase after reloadData - objective-c

I want to calculate the size of a cell that I've just added to an NSOutlineView. To do that, I add it to my data source, and then call reloadData on the NSOutlineView. For some reason, however, I can't examine the cell after reloadData for any but the very first row. The code looks like this:
[m_dataSource add:...]; // first add item to my data source
[m_outlineView reloadData];
int nRows = (int) [m_outlineView numberOfRows];
NSCell *cell = [m_outlineView preparedCellAtColumn:0 row:nRows-1];
NSSize size = [cell cellSize];
For some reason, nRows is always 1 here, no matter how many rows I add. Thus, I can only ever query the size of the first cell in the very first row.
The behaviour I see might be related to the fact that the very first row is an expandable row and all other rows are children of the very first row. However, I have checked that
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item;
correctly returns YES for the item. Everything also appears correctly in the NSOutlineView. It's just that numberOfRows doesn't seem to get updated even though I call reloadData just before it.
Any ideas what's wrong here?

Related

how to remove accessory from cellForRowAtIndexPath

i have the indexPath number... i need to remove the cell accessory from that cell based on the number in the variable i have?
NSInteger *removeAccessoryFrom = 3;
[studentTableView cellForRowAtIndexPath:removeAccessoryFrom].accessoryType = UITableViewCellAccessoryNone;
It doesn't work like that. You need to reload the table data (either for the whole table or for just this row) and, inside cellForRowAtIndexPath, set the accessoryType to UITableViewCellAccessoryNone conditionally. In other words, you keep a list, or have some other way of knowing, at every moment, and for the entire table, exactly what cells get exactly what accessory. That way, any time cellForRowAtIndexPath is called, you can configure this cell for this index path correctly.

Save temporary values deleted by reloadData in a UITableView

I have a simple project with UITableViewCell, inside the Cell I have a UITextField, In my case I am creating rows of a table dynamically, ie:
When I call function 'AddField' There is a counter that is responsible for storing the number of rows that the table must have.
-(void)AddField{
numberOfRows++;
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return numberOfRows;
}
So, if I type something in the first text field and then call the function to add more rows in the table, the row is added, another text field appear (leaving two), but the text entered in the first text field disappears due the command reloadData.
I Need help find the best way around this problem, any suggestions?
Have a Mutable array to store all the typed values.
[self.myMutableArray addObject:cell.textField.text];
In cell for Row at index path, get the text and set it back to the text field.
cell.textField.text = self.myMutableArray[indexPath.row]

How to detect how many cells are visible in a UITableView

My table view covers part of the view. If I have 20 objects, I am displaying all of them in a tableview. But I want to know that how many cell are loaded that are visible to the user.
(i.e. first 5 cells data is visible for me: when I scroll down, the remaining cells will load. Here I want to know without scrolling how many cell are loaded.)
Is this possible?
You can use [tableView visibleCells] count], which returns numbers of the table cells that are visible, if I understand correctly what you want.
The below code will give you the array of indexpath of cells currently visible
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
after getting the index path you can easily access the cell at a particular indexpath
Its usually : (tableView's height / cell row height ) + 1
Can you take the size of the visible part of the table, and then the size of one cell, and divide them to know how many of them fit in the screen?

UITableViewCell's disappear after insertion/deletion

I have a few UITableView's in my project, and sometimes I need to swap cells between them. Cells are complex enough and it's take a lot of time to re-create them each time I move them from one table to another, so I create its once, keep in array and pick up necessary cells  in cellForRowAtIndexPath Moving cells between tables looks something like this:
NSIndexPath* pathForMovedCell = [firstTableView indexPathForCell:movedCell];
[firstTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathForMovedCell] withRowAnimation:UITableViewRowAnimationFade];
//There is some delay for show animation and switch between view controllers
[secondTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:pathForMovedCell] withRowAnimation:UITableViewRowAnimationFade];
This code works pretty good, but sometimes (always, if delay less then 0,33 seconds) cell is not drawn on secondTableView. It normally inserted, and will be drawn if scroll them from visible area and return back. I think it's happens if inserting occurs before deletion animation ends. If I made short delay, cells disappears often, and I don't want to leave big time reserve, because it looks like lags, so I want to solve this problem correctly. I tried to add [cell setNeedsDisplay] before returning cell from cellForRowAtIndexPath, but it doesn't work.
Any suggestion?

reloadTable after animated row deletion

I have a table view with cells that can be deleted. When a cell is deleted, I would like to display a deletion animation such as UITableViewRowAnimationFade. The problem is that my table view's cells have alternating background colors, so I need to -reloadData after the animation completes to fix the colors of all cells below the deleted cell.
So, incase you haven't read this far, I need a way to run some code after a table view animation is complete. I am currently using performSelector:withObject:afterDelay: and hardcoding an approximation of the animation delay. This method works, but there must be a better way to do it:
NSIndexPath * path = [listTable indexPathForCell:deletingCell];
NSArray * indexPaths = [NSArray arrayWithObject:path];
[listTable deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[listTable performSelector:#selector(reloadData) withObject:nil afterDelay:0.35];
In this case I am assuming that UITableViewRowAnimationFade will take less than 0.35 seconds to complete.
You could also use NSTimer with the same delay (although unneeded, -performSelector:withObject:afterDelay: works fine).
Another option would be to loop through the cells after deletingCell (path) and redraw them, or add the index paths to an array and call reloadRowsAtIndexPaths:withRowAnimation: as shown below. Then you could also use the UITableViewRowAnimationFade
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation