I need to have a custom cell height for all of my cells in my UITableView. In this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
I try this:
if ([[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] == #"imageCell")
As I have 3 different cells setup with different identifiers in my storyboard. However my app just crashes here with EXC_BAD_ACCESS.
Any idea why?
Thanks.
You're comparing a string, so you should be using isEqualToString:
if ([[[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] isEqualToString:#"imageCell"])
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
you are supposed to calculate the height for you row, not to get the Cell of your tableView.
In my opinion, in the lifeCycle of a TableView delegate, the 1st step (before allocating each UITableViewCell), the TableView delegate call heightForRowAtIndexPath for each row (but at this moment, the UItableViewCell are not allocated). The, in a 2nd step, the TableView call
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to create UitableView Cells.
Related
Anyone can encountered this,
Set static tableview and label but once I run it, no words appear as in ALL Blank.
It's basic but I can't figure out the reason.
Thank you in advance :)
you will have to implement tableview delegate and datasource
[self.tableView setDatasSource:self];
[self.tableView setDelegate:self];
you may code it in viewDidLoad then implement following datasource.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
also you don't need similar cells again, you can simply reuse one cell using an identifier as follow.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"reuseIdentifier"];
I have list of complex UIViewControllers. This list should be displayed in vertical form. I'm wondering how can I display them. I tried UIPageViewController, but it's showing just one child controller. I need to show them as many as can fit on screen (like UITableView or UIScrollView). I cannot use UITableView, because it doesn't support nested UIViewControllers. So do I have to use UIScrollView and implement own releasing mechanism for child controllers or is there any other way?
You can use UITableView:
in tableViewDelegate put
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//calc cell height
return height;
}
than in this method do like there:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:COMMON_CELL_IDENTIFIER];
[self addChildViewController:viewControllers[indexPath.row]];
[cell.contentView addSubview:[viewControllers[indexPath.row] view]];
return cell;
}
I am trying to work with UICollectionView that is a part of the UITableViewCell.
So the issue I faced, the collection view for visible table cells works ok, but when I start scroll table and table starts create new cells that where invisible then I faces with some behaviour that duplicate first table cell. For example I have scrolled collection view in first table view cell, then I scrolled table view down, and what I see the new cell have the same state as my first cell had. You can check source here repo and the video here to understand what I am talking about.
Have a property of UICollectionView in CustomTableViewCell.h and bind it in Main.storyboard. Let's assume declared it like this:
#property (strong, nonatomic) UICollectionView *collectionView;
In ViewController.m change cellForRowAtIndexPath method, so it reloads inside UICollectionView every time it need
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomTableViewCell"];
[cell.collectionView reloadData];
[cell.collectionView scrollRectToVisible:CGRectZero animated:NO];
return cell;
}
Of course, you need to import CustomTableViewCell.h on ViewController.m file.
-[UITableViewCell prepareForReuse]
Problem with reuse identifier in UITableView cell, if you use single cell identifier in cellForRowAtIndexPath this problem will occurs
1. Use Dynamic cell identifier for each row, like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CustomTableViewCell_%d",indexPath.row]];
}
2.Store content offset for each collection view in array, and reset collection view content offset
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
......
[cell.collectionView setContentOffset:scrollingPoint animated:NO];
return cell;
}
I have gif animated pictures in some uitableview rows,
When there is a lot of gifs in tableview CPU usage is going to be very high,
So I want to hide these gifs when their row is not visible,
How can I do that ?
How can I achieve not visible cells row's indexPath ?
After that I can hide the gif like that :
UITableViewCell *celll = [ tableView cellForRowAtIndexPath:indexPath];
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = TRUE;
So I must get not visible cells indexPath's in a loop.
Generally, you don't need to do that as long as you properly use reusable cells.
Nevertheless, if you do want to do that, you can use tableView:didEndDisplayingCell:forRowAtIndexPath: method on UITableViewDelegate:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = YES;
}
If you'll put the line:
NSLog(#"%#",indexPath);
as the first line of the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You'll realise that it's done automatically. Only the visible cells, i.e the one that appears on the screen, are the one you present the data for.
Sorry for poor english.You had not posted code about how your cellForRowAtIndexPath implementation look like,i will suggest to make your cellForRowAtIndexPath like below.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"YOURIDENTIFIERSTRING";
UITableViewCell *cell = [tableView dequeReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
// Configure the cell here …
return cell;
}
This way only those many cells will be created which are visible on device and once row goes out of device view it will be reuse for new row. Please check apple doc for tableview.
I have a UITableView with some delegate methods.During load for the first time it's all ok, but during the rotation I saw that the method cellForRowAtIndexPath doesn't recall. Why?
My delegate method are:
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
//.....
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//......
}
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//...This method is not called during the rotation of the device...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//.......
}
You need to refresh table data manually after the rotation occurs using [tableView reloadData] within shouldAutorotateToInterfaceOrientation method.
That method is only called when the cell is about to become visible on the screen and the table requests it. Once it is in the view, it will not be called again until it has scrolled out of view and then back.
You have 3 options for handling the rotation:
Use AutoLayout
Use Springs & Struts
Override layoutSubviews (which will get called on a rotation)
What you choose will depend on how complex your cell layout is. Also, make sure the table resizes on the rotation, otherwise the cells won't see a size change.