iOS UITableView reloadRowsAtIndexPaths - ios

I have a UITableview that lazy loads images of all different sizes. When an image loads, I need to update the specific cell, so I figured out I need to use reloadRowsAtIndexPaths. But when I use this method, it still calls the heightForRowAtIndexPath method for every single cell. I thought the whole purpose of reloadRowsAtIndexPaths is that it will only call heightForRowAtIndexPath for the specific row you specify?
Any idea why?
[self.messageTableView beginUpdates];
[self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.messageTableView endUpdates];
Thank You

endUpdates triggers a content size recalculation, which requires heightForRowAtIndexPath. That's just how it works.
If it's a problem, you could pull your cell configuration logic outside of cellForRowAtIndexPath and reconfigure the cell directly without going through reloadRowsAtIndexPaths. Here is a basic outline for what this could look like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = ...;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
[self tableView:tableView configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//cell configuration logic here
}
Then, wherever you're currently calling reloadRowsAtIndexPaths, you do this instead and heightForRowAtIndexPath won't be called:
UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
[self tableView:self.messageTableView configureCell:cell atIndexPath:indexPath];

Related

UITableView with .xib cell with error : failed to obtain a cell from its dataSource

I have create table cell with .xib file. and added in the UITableView using following code :
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:#"desplayCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"DisplayCell" bundle:nil] forCellReuseIdentifier:#"desplayCell"];
}
//
return cell;
}
App crashes with error :
failed to obtain a cell from its dataSource
I forget to add dequeueReusableCellWithIdentifier in your code.
dequeueReusableCellWithIdentifier usage
You should use the same reuse identifier for all cells of the same form.
The reuse identifier is associated with those cells (rows) of a table view that have the same general configuration, minus cell content.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:#"desplayCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"DisplayCell" bundle:nil] forCellReuseIdentifier:#"desplayCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"desplayCell"];
}
//
return cell;
}
You can use like this
In viewDidLoad write below code
[tableView registerNib:[UINib nibWithNibName:#"DisplayCell" bundle:nil] forCellReuseIdentifier:#"desplayCell"];
In cellForRowAtIndexPath you can write below
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DesplayCell *cell = [tableView dequeueReusableCellWithIdentifier:#"desplayCell"];
if (cell == nil) { // if cell is nil then you can alloc it
cell=[[DesplayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"desplayCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"desplayCell"];
}
return cell;
}
The answer is like Siddhesh Mhatre post But you should make a category of UITableView, it's useful for a large project.
UITableView+Category.h
- (id)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier;
- (id)dequeueReusableOrRegisterCellWithClass:(Class)aClass;
UITableView+Category.m
- (UITableViewCell *)dequeueReusableOrRegisterCellWithIdentifier:(NSString *)identifier{
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
[self registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];
cell = [self dequeueReusableCellWithIdentifier:identifier];
}
return cell;
}
- (UITableViewCell *)dequeueReusableOrRegisterCellWithClass:(Class)aClass{
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)];
if (!cell) {
[self registerClass:aClass forCellReuseIdentifier:NSStringFromClass(aClass)];
cell = [self dequeueReusableCellWithIdentifier:NSStringFromClass(aClass)];
}
return cell;
}

UITableView "cellForRowAtIndexPath" not working

I'm trying to use "reloadData" method on a uitableView, it seems like it works (both dataSource method are being called) and when I debug "cellForRowAtIndexPath" the cell that it returns is the correct cell) but I can't see the cell (numberOfRowsInSection is changing and adding blank space for each new cell - So it's not seems to be a threads problem). For example, if I have in the tableView 5 "names" (my data array called "namesArray") and I add 2 there will be 5 names and 2 nil cells presented on the tableView. The problem seems to be with cellForRowAtIndexPath.
cellForRowAtIndex:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
NSString *cellIdentifier=#"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];
//Returning cell
return cell;
}
numberOfRowsInSection:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.namesArray.count;
}
-(void)addNameToNamesArray:(NSString*)name
{
[self.namesArray addObject:name];
NSLog(#"namesArray lastObject: %#",[self.namesArray lastObject]);
//NSLog print: The correct name (the one that was added)
[self.tableView reloadData];
}
screenshot for "5 names and 2 nil cells":
Do you have any idea what can it be?
You call
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
inside cellForRowAtIndexPath!
Don't you think that's a bit late? And if it worked, you would be calling it a bit often?
And if dequeue... returns nil when a class is registered, something is badly wrong.
change to
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
static NSString *cellIdentifier=#"Cell";
//[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];
//Returning cell
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.namesArray.count;
}
set the outlet of tableview and then in addNameToNamesArray method after adding elements reload the tableview
This is likely because you have updated the data in your array, but the table view hasn't accessed that data yet, leading to the nil cells that you see. Since your cells load properly with the initial 5 element in your array, it's probable that you simply haven't refreshed the table view's data. Try using [tableView reloadData] after you have added the new elements to your array.
EDIT 1
What about changing your cellForRowAtIndexPath code to this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Creating "cellIdentifier"(NSString) with "Cell" as the value
NSString *cellIdentifier=#"Cell";
//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.text = [self.namesArray objectAtIndex:indexPath.row];
NSLog(#"name: %#", cell.textLabel.text);
//Returning cell
return cell;
}
Let us know the output of NSLog.
1.Make sure you have initialized self.namesArray in viewDidLoad
Try the following:
[tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:cellIdentifier];
like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
return cell;
}
Your code is perfectly alright. There can be some possibilities that its not working for you.
You are not initialising your datasource i.e NSMutableArray in your case.
It depends how you are calling addNameToNamesArray method.
If you can share your whole code for that class as how you are calling method and every delegates you are using.

Why UITableView is not dequeuing reusable cells?

I'm adding subviews with UITableViewCell default type. In this, assume that, I've only 2 rows. At first, when it comes to tableView:cellForRowAtIndexPath: I'll generate a dynamic identifier for each cell based on indexPath.row. So it goes well and create a new cell inside if(cell == nil) condition. I'm adding subviews inside this condition only.
Next, out side that condition, I'm simply updating content of subview by fetching them out of the cell's contentView. This works great.
Problem is here : I'm expanding my cell (on which user tapped). When I do this, it'll go into tableView:cellForRowAtIndexPath: and I'm able to see the same identifier generated before. However, its going inside if(cell == nil) condition.
For a note, I'm checking for that condition like this,
if(!cell) and not if(cell == nil).
Can you guess what is the issue? However in my deep investigation, I found that, it'll only goes inside if(cell == nil) once I expand particular row and not next time for the same row.
Here's the sample :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//number of count
return [array count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//dynamic height will be provided based on content.
return [self heightForRow:indexPath];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [NSString stringWithFormat:#"cellIdentifier_%ld", (long)indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setupCell:cell forRow:[indexPath row] inTable:tableView];
}
[self updateCell:cell forRow:[indexPath row] inTable:tableView];
return cell;
}
- (void) setupCell:(UITableViewCell *)cell forRow:(NSInteger)row inTable:(UITableView *)tableView {
//setting up view here
}
- (void) updateCell:(UITableViewCell *)cell forRow:(NSInteger)row inTable:(UITableView *)tableView {
//updating view here
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.expandedIndexPath = ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) ? nil : indexPath;
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[tableView scrollToRowAtIndexPath:self.expandedIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
Hope my issue is clear to you. Please comment if you don't get something or found anything missing here (and I'll update it here) or answer if you know what's the solution to this.
Thanks!

Add UIView to reused UITableViewCell?

I am desperately trying to achieve the following effect: I have got a tableview where I would like to expand the cells on selection and show some additional information on it. I reuse table cells, just FYI. In the past I was able to achieve such an effect in another application, but this just does not seem to work for me at all. All the methods that are vital for this to happen are called and the row expands beautifully, but the additionalInfo table in the showExpandedContents method just will not appear!
If I create the additionalInfo table in the initialisation of the cell, the table will appear but when I then try to load the data (and by that, also the cells) it just won't reload the table.
But the method that is responsible for this (showExpandedContents) table is definitely called.
If I initialise AND set up the table in the initialisation code of the cell everything works fine.
So here is some code for you:
Here is the cell setup:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
((MyCustomCell*)cell).data = [arrayToDisplay objectAtIndex:indexPath.row];
return cell;
}
Here is the selection method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//setting the current cell as the expandedRow, so that I can collapse it later
expandedRow = [tableView cellForRowAtIndexPath:indexPath];
[((MyCustomCell*)expandedRow) showExpandedContents];
[tableView beginUpdates];
[self changeRowHeightAtIndex:indexPath.row height:330];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
Here is the -showExpandedContents method in MyCustomCell where I would like to display the additional information:
-(void)showExpandedContents{
additionalInfo = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.frame.size.width, 250)];
[additionalInfo loadContent];
additionalInfo.backgroundColor = UIColorFromRGB(0xf9f9f9, 1.0);
additionalInfo.layer.zPosition = MAXFLOAT;
[self.contentView addSubview:additionalInfo];
}
Looks like your
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
is giving you a fresh cell, different than the one that had showExpandedContents called.
Removing this line should fix the problem.

ios - Expand table view cell like Twitter app

I want to have the same behavior as Twitter app when selecting a tweet that is : expanding the row and adding supplementary content.
So this is not only a basic row resize. I think i need 2 different custom cells, so i did something like that :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath compare:self.selectedIndexPath] != NSOrderedSame) {
FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FirstCell"];
if (!cell) {
cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FirstCell"];
}
cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:#"firstname"];
return cell;
}
else {
SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SecondCell"];
if (!cell) {
cell = [[SecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SecondCell"];
}
cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:#"firstname"];
cell.lastnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:#"lastname"];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) {
return 80.0;
} else {
return 44.0;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
My problem here is that i want to keep a fluid animation like Twitter app which is not my case because of [tableView reloadData] (which is mandatory i think since i have 2 different custom cell).
So anyone knows a workaround to my needed or maybe someone knows how Twitter app is handling this animation stuff ?
You want to reload that cell with an animation:
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
[tableView beginUpdates];
[tableView endUpdates];
this triggers an update of the cell row sizes for the whole tableView.... referenced from :iPhone - Smooth animation for UITableViewCell height change including content update
Happy Coding!
It is actually rather easy to implement this using two custom cells like you originally intended by just combining your original code with rooster117's solution. Replace [tableView reloadData] in your tableView:didSelectRowAtIndexPath: with:
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
and you should have your solution.

Resources