Why does this line indefinitely repeat? - iphone

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCellFixed *cell = (UITableViewCellFixed *)[tableView cellForRowAtIndexPath:indexPath];
the second line (ie: the first line after the function is declared) is repeating itself indefinitely. I have no idea why.
I got a stack overload when I was running the script, and when I put a breakpoint on this line and subsequent lines in the same method this line just continuously repeats and the breakpoints never move onto the next one.
I have no idea why this is, but this is my first time trying to subclass a UITextViewCell, so I'm assuming I've done something wrong?
Thanks
Tom

When you ask your table for a cell, the table itself calls heightForRowAtIndexPath to be able to display the cell in correct height.

Related

cellForRowAtIndexPath Usage Pattern

In the below code I want to perform some DB actions when a cell is deleted, therefore I need to send my Server information about the cell being deleted. If I remember correctly cellForRowAtIndexPath should never be called directly, However I cannot think of any other way to get cell info in the below method, so my question is:
Is it acceptable to call cellForRowAtIndexPath manually below:
[tableView cellForRowAtIndexPath:indexPath]);
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[localGlobalNotifications removeObjectAtIndex:indexPath.row];
[notificationTableView beginUpdates];
[notificationTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self postLeaveRequest];
NSLog(#"Row is : %#", [tableView cellForRowAtIndexPath:indexPath]);
[notificationTableView endUpdates];
}
}
To clarify: I understand that I can invoke a delegate call of cellForRowAtIndexPath by calling reloadData, what I'm trying to do is access the cell being deleted within commitEditingStyle. I'm not trying to reload my tableView, instead I want to get a reference to the cell being deleted. - Is it acceptable to obtain a reference to said cell by calling cellForRowAtIndexPath directly?
There's nothing wrong with asking the TableView to give you the cell, just as you do in your sample code.
Here's the documentation for the return value:
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
If you're annotating the cell with 'model' data then I think you're breaking the MVC pattern. Your view doesn't need to know about the model data in this way, and so querying the view to make a database change will make life difficult you in the future (readability, extensibility and reusability for example)
You would be better off having your DB metadata stored in a collection such as an NSArray - or an NSArray of NSArrays.
Then you could get all the data you need with something like:
id modelData = myModel[indexPath.section][indexPath.row];
Yes you can use cellForRowAtIndexPath: as they have also used in apple docs or you can create an array of your cells then you can delete it from there and reflect it in your database.
You can call cellForRowAtIndexPath: method using following line, here it will automatically calls all delegate and datasource methods.
[tableView reloadData];
or
[tableViewObjct reloadData];
Hope this solves your problem!

UITableView not able to fetch data from array

i am making iPad application, in which i am fetching data from Url, after fetching data from URL,
i am storing into array,
when i write NSLOG inside this two TableView method,
it works properly,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLOG(#"ARRAY=%#",arrayname);
}
but when i write this NSLOG inside this method thn it shows error,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
i also declared nsmutablearray properly....
it shows EXC_BAD_ACCESS
why this any idea ?
The method cellForRowAtIndexPath must return the cell, otherwise you'll see the error you see. But if the code above is 'metacode', and you put you NSLog somwhere between the proper strings of code, then you can follow Alex Reynolds' advice.
Check if your array is still alive. E.g. you can set a breakpoint and check if the program falls because of turning to 'arrayname', and if so, try to find where it could be released in your code by this moment. Also, perhaps you're just create your array with wrongly.

What should cellForRowAtIndexPath return if the table is empty?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I think if the table is empty, namely that
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[BNUtilitiesQuick getBizs] count];
}
always return 0
I would expect that -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath should not be called at all
This is important. I am making a program to search through things and sometimes after some searching, there is no result being returned.
However, is called anyway and I got an exception. What should I do?
-tableview:cellForRowAtIndexPath: may be called before the table view has realised it has zero rows and so shouldn't be called.
Therefore your implementation needs to check that the value of the row being passed in is not outside the bounds of your array. If it is, you need to return an empty cell (the documentation states returning nil will raise an exception).
There is simply no way that tableView:cellForRowAtIndexPath: will be called when the tableView:numberOfRowsInSection: method returns 0. It is likely that the count is being returned incorrectly.
Often you would have a giant switch statement in a cellForRow... Meaning that you have to have a value to return in the default case. I usually return nil there.
This is required by the compiler to not show a warning, but it should never be actually called. This method is only called for row indexes that are possible due to what you reeturn in the numberOfSections and numberOdRowsInSection methods.
First print [[BNUtilitiesQuick getBizs] count];
in NSLog and see if it really returns 0.
If it shows 0, and if the cellForRowAtIndexPath: still gets called, I suspect there is some ghost hanging around there. :-)
What about returning 1 as count (or 10 for example), and that one cell will display dimmed text with "No Results" ?
(open your contacts app and search for something.. it will have about 10 rows , 9 are empty and 1 displays "no results")

Flushing UITableView queue for cells

I was wondering if some one can please reply me with if the UITableView queue gets flushed when UITableView reloadData is called.I am trying to do so and this isnt helping me.Any suggestions?
if you look at the header file for UITableView, you can see that there is a private NSMutableDictionary (iVar) called "_reusableTableCells". This is a dictionary with cell reuse identifiers as key and with array with cells that are currently offscreen as value.
If you want to manually flush queued cells in way that may change with the implementation, you can do so in ugly manner, like so:
NSMutableDictionary *cells = (NSMutableDictionary*)[self.tableView valueForKey:#"_reusableTableCells"];
[cells removeAllObjects];
Hope this helps...
Once the table is loaded, the cells are reused. Reloading the table does not flush the queue. reloadData calls - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
In this method the if(cell==nil) condition is present so that cells are not flushed once they have been loaded into memory, and therefore reused.
To get around this you reset your cells before applying the correct information.
cell.detailTextLabel.text = #"";
cell.accessoryType = UITableViewCellAccessoryNone;
Or if you are using an accessoryView
cell.accessoryView = nil;
Also take a look at this example. UITableView not updating correctly when scrolling.

iPhone/Obj-C - Archiving object causes lag

Hi I have a table view, and when I delete a cell I am removing an item from an NSMutableArray, archiving that array, and removing the cell.
However, when I do this, it is causing the delete button to lag after I click it. Is there any way to fix this?
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
int row = [indexPath row];
[savedGames removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//this line causing lag
[NSKeyedArchiver archiveRootObject:savedGames toFile:[self dataFilePath]];
}
}
Thanks
It sounds like you know the answer already -- don't archive everything on every delete. This can be done a number of ways -- archiving only pieces at a time, delayed archiving (periodically/when quitting/other policies), or making your custom archiving code considerably faster, which I doubt would even help that much in the scheme of things. I've heard that MAKeyedArchiver was faster than NSKeyedArchiver, but I believe this was some time ago, and designed for the mac+potentially platform specific (on the bright side it was intended as a drop in replacement for the NSKeyedArchiver API of the time, so it should be little integration time if you choose to use it).