Reputation: 33
I am new to iOS. I am practicing objective-c and made my custom UITableViewCell. It has some height rather then its default height i.e height is 120. But when I run the program, cells are shown on their default height. I only have written some code as follows and I am using xib to design custom cell. I can share screenshot if required. Thanks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
Edit In one of my project I have not implemented heightforrowatindexpath method. Why should I have to implement in another project. Is is necessary?? Please guide.
Upvotes: 0
Views: 91
Reputation: 468
If all your custom Cells are of the same height but different from the default height, you have three options to define the height.
1) Define it as a property of TableView in storyboard/Interface builder. Select TableView -> Size Inspector -> Row Height.
2) In your viewDidLoad(), you can define the height _tableView.rowHeight = 120;
This option overrides the value in storyboard/Interface builder.
3) You can implement the delegate as others too have suggested.
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 120;
}
This last option gives you flexibility to define different heights for each row and overrides values from above two options.
There are ways to get dynamic cell height based on content. Let me know if you need a hand on that.
Upvotes: 1
Reputation: 2668
The height of the row must be returned in a delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 120.0;
}
Upvotes: 0
Reputation: 3447
If you want different cell heights you need to provide them in a delegate: https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview?language=objc
Upvotes: 0