Two UITableViewCell with different identifier and different height in storyboard - iphone

I have two type of cells that I'll be using my UITableView, so I created two prototype cell with different identifier. I manuelly changed the size but when I compile and run, the two cells have same size.
Is there any way to do it through storyboard and without checking every single time
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
I will have around a good 100 cells at least.
UPDATE: in my case I have a tableview with 3 sections, the first one is small, the second and the third one are bigger.

I think your best option is to use that -tableView:heightForRowAtIndexPath: delegate method you listed and return the height you would like.
You could assign tags to each prototype and use an if/else if conditional.
Or if you have a subclassed UITableViewCell for each prototype you could do something like
...
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[CustomCell1 class]]) {
// return aNumber;
} else if ([cell isKindOfClass:[CustomCell2 class]]) {
// return aNumber;
} else {
return aNumber;
}
...

If you know that the sections if what determines the height of the cell, then you can implement it like this:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
CGFloat result;
if (indexPath.section == 0) {
result = 160;
} else {
result = 80;
}
return result;
}
Depending on the heights that you need of course.

Related

Change heightOfPrevious UITableViewCell

Is there anyway I can change the height of previous UITableViewCell when setting height for current cell in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath?
Have two rows in a UITableView. When in first row, it will set the default height.. When in row two I want to change the height of first row. Is this possible? If so how?
Thanks,
Bharathi.
Try to work with a boolean check if the requirement is fulfilled.
You could do it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 && !secondRowIsSynced) {
return otherHeight ;
}
else {
return defaultHeight;
}
}
I think you misunderstand tableView:heightForRowAtIndexPath: is called multiple times (once for each row in your table).
If your rows have different heights then you need to determine which row you are on and what height it should have.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGFloat height;
if (0 == indexPath.row) {
// This is the first row
height = // what ever you want
} else if (1 == indexPath.row) {
// This is the second row
height = // what ever you want
}
return height;
}
If you later decide that a row needs to be a different height then it is still this method that needs to calculate the correct height to be used for each row. You can force this to be called without reloading the tableView like this
[tableView beginUpdates];
[tableView endUpdates];
bharathi:
You have to reload your table in the didSelectRowAtIndexPath method of UITableView.
You should check that the second row is selected, then reload all the table rows, so all tableview delegate and datasource methods get called again.

When click on button (button in cell) that particular cell should be changed?

There are 1 table on uiview and I want to change the cell height when button pressed other cell's height remain same
Pass the button press event to the view controller through delegate methods and reload the table view as follows.
[self.tableView reloadData];
In view controller (ie., datasource for the table view), implement heightForRowAtIndexPath method and return the height as required.
You can change the cell height in the delegate method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == clickedRow)
return newHeitht;
return cellHeight;
}
you can set some condition in button click and reload the tableview using [tableView reloadData]. this function will be called. return a new height for the particular cell.
-(void)buttonClick {
[self.tableview reloadData];
selectedRow = //do something here
}
and in your UITableview Datasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row == selectedRow)
return selectedRowHeight;
else
return defaultHeight;
}
1) Create a custom UITableViewCell class.
2) Populate you cell however you see fit. I use a 'hydrateWithArray' type function myself.
3) Once the data has been populated, resize and reposition elements using 'sizeToFit' functions to force labels to conform to the size of whatever you put into them. protip: by setting the frame of the label first, and setting the numer of lines to 0... when you fill the text and sizetofit, it will stretch the label vertically only and force the width to stay the same.
4) Create a seperate function (mine is called calculatedHeight) that returns a float and returns the height that you would like the cell to be in the table (based on the repositioned objects from step 3).
- (float)calculatedHeight {
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}
5) In your UITableView class, you'll need to import your tableViewCell class and create a dummy cell object. You're going to use this class to calculate how tall each cell needs to be. Then in the heightOfRowAtIndex method....
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
if ( !dummyCell ) dummyCell = [[CustomCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:#"myCell"];
[dummyCell hydrateWithTweet:[tableArray objectAtIndex:indexPath.row]];
height = [dummyCell calculatedHeight];
if ( height == 0 ) height = 50;
return height;
}
This is a pretty simple example so you may need to go crazy with the error checking in your particular use, but this should at least point you in the right direction. Enjoy!
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
else {
return rowHeight;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];
then
[self.tableView beginUpdates];
[self.tableView endUpdates];

Expand and collapse static table view

I have a static table view in my application. This table view is used for preferences.
A section in the table view only has one cell which holds a UISwitch. When this switch is activated, I want to show the section beneath and when it is not, I want to hide the section beneath. All the sections (also the one which should be hidden / shown) is set up using Interface Builder.
Is there any way to hide or show this section when the table view is static as a static table view doesn't have a data source? Should it be easier, I could also agree to use the same section but add / hide rows from this section when the switch is on or off.
EDIT
I have come closer how to do this.
Setting the height of the cells in the section and the height of the footer and header of the section to 0, I can nearly hide the section. I still have some spacing between the section above and the section below that I cannot figure out how to get rid of.
Does anyone have an idea where this extra spacing comes from? See the photo below.
This is the code I use to nearly hide the section.
/* Will display cell */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2)
cell.hidden = YES;
else
cell.hidden = NO;
}
/* Height of cell */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2)
return 0;
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
/* Height of section header */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 2)
return 0;
return [super tableView:tableView heightForHeaderInSection:section];
}
/* Height of section footer */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == 2)
return 0;
return [super tableView:tableView heightForFooterInSection:section];
}
This is how the table view looks now. There is still some space I need to hide. The extra space is between the sections labeled "Arbejde" and "Anden".
I got it working using the code in the question. Just set the height to 1.0f instead of 0. It seems that the height only has an effect when it's value is greater than zero.
Reducing the space between sections of the UITableView.
For static cells, I just use this:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let cell = self.tableView(self.tableView, cellForRowAt: indexPath)
if cell.isHidden {
return 0
} else {
return cell.bounds.size.height
}
}
see this tutorial this may helps you www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
For static cells I found no decent answers either. If you wish to drop sections or expand a cell then this is the code I used.
Create a BOOL Value in your .m
#interface yourClassName (){
BOOL ddMenuButtonPressed;
}
#end
ddMenu means drop down menu.
Next I set the bool value to false in the viewDidLoad method.
ddMenuButtonPressed = false;
I then initialise the ddButton (in storyboard control+drag from your button or switch to your .h file and create action and set name to ddMenuButton) and use this method in my .m file.
- (IBAction)ddMenuShow:(UIButton *)sender
{
if (sender.tag == 0) {
sender.tag = 1;
ddMenuButtonPressed = true;
} else {
sender.tag = 0;
ddMenuButtonPressed = false;
}
//very important that you include these
//they update the view like a viewDidLoad statement without leaving the screen
//if you have any data entry points in your cell like a textfield it **will not** erase that data fortunately
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
Finally we add the following method:
// Handle expanding and minimising cell or section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//if the section is 0 (and there is only one cell) and ddMenuButtonPressed hasn't been pressed
if (indexPath.section == 0 && ddMenuButtonPressed == false){
//return the height you have it set as in story board (or a number)
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
//if the section is 0 and the ddMenuButton has been pressed
if (indexPath.section == 0 && ddMenuButtonPressed == true){
//change the cell height to 380 or whatever size you want
return 380;
}
//otherwise leave cells as they are
else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
And that's it. No fancy coding, nice and clean (minus the commenting) and simple and you can use this over and over for as many sections as you like.

How to remove a Static Cell from a UITableView designed in StoryBoard

The solution is probably very simple, but I couldn't just find it .. !
Working with storyboard (iOS 5), I have a tableViewController, and a designed STATIC tableview with 5 sections, with differents static cell inside each section.
My question is: How to delete a cell programatically in the viewWillAppear?
For example, I have a cell designed for a date
IBOutlet UITableViewCell * cellForDate;
And.. if there's not date, I want to remove my cell.
cellForDate.hidden = true; //Hide the cell, but leave a blank space
Ive tried [tableView deleteRowsAtIndexPaths...] didn't work
Anyone got an idea?
Try to hide the cell before it is shown, in UITableViewDelegate's tableView:willDisplayCell:forRowAtIndexPath: method. That's the final method where you can manipulate the cell's appearance. This however won't remove the space the cell should take, so another thing you can try is to set cell row's height to 0 using the tableView:heightForRowAtIndexPath: method of the same protocol.
Another method, which is most robust, is to devise a method for determining whether you need the date cell in the section and depending on the result, return the proper number of rows in the section, and return other section row cells taking the situation into account. And then reload tableView's data on viewWillAppear.
Hopefully this is a bit more of a universal solution, but it's still not perfect
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell.hidden) {
return 0;
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
With this method you set your IBOutlet bound cell to be hidden, and then when the table tries to display a row, it first checks to see if the cell (using super call to get the cell) should be hidden, if so it returns a height of 0 which effectively removes it from the list. If the cell shouldn't be hidden then it gets the height from the super so whatever would normally happen still happens.
The only issue with this is that start and end cells are responsible for the divider at the top and bottom of the list or group. So if you hide the last cell in a group the bottom divider on the group will be inset slightly like it is on normal rows rather than full width. This is only a problem if you're hiding the top or bottom rows AND you are using a divider AND you care about totally standard display. The best solution in my case was simply to not hide the top or bottom rows. I moved any content that might be hidden to the middle of the group. Alternatively you could just accept that it isn't quite standard, or disable row dividers on your table.
Swift 3
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
cell.isHidden = true
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 0
} else {
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
If you want to permanently delete the tableview cell, just call deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone] with the an indexPath corresponding to your row. You will also need to decrement the number of rows returned by numberOfRowsInSection:.
However, I was looking for a way to temporarily "delete" unwanted static tableview cells from a static tableview, because I wanted a particular row to be there sometimes, and other times not. I also wanted to be able to update this on demand from outside of the tableview controller.
My case is fairly simple, since it is the first row that was being either shown or hidden. You can generalize to suit your own needs. My tableview controller is my data source delegate.
First, I created a public method in the tableview controller to update the state variables and trigger the redisplay:
- (void)updateSettingsDisplay:(BOOL)hideSetting
{
if (hideSetting == self.hideSetting) {
return; // no need to do anything because it didn't change
}
self.hideSetting = hideSetting;
self.numRows = (hideSetting)? kNumRowsWithSetting : kNumRowsWithoutSetting;
// redisplay only if we're visible
if (!self.viewJustLoaded && (self.navController.visibleViewController == self)) {
[self.tableView reloadData];
}
self.viewJustLoaded = NO;
}
The tableview controller's viewDidLoad looks like:
- (void)viewDidLoad
{
[super viewDidLoad];
// check whether or not to display out-of-coverage tableview cell
self.hideSetting = YES; // initial value; could just as easily be NO
self.viewJustLoaded = YES;
[self updateSettingsDisplay];
}
The tableview's data-source delegate:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.numRows;
}
and finally
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set row to the index of the stored tableView cell that corresponds to the
// cell you want (its location in the static tableview from your storyboard)
NSInteger row = indexPath.row;
if (self.hideSetting) {
// skip drawing setting's tableviewcell (since the setting we're hiding is
// first, we can just increment the row to get the one we want)
++row;
assert(row < kTotalRows); // bounds checking just to convince yourself (remove after testing)
}
// get a new index path since the row field is read-only
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
// grab the cell from super that corresponds to the cell you want
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:newIndexPath]; // static table
return cell;
}
The trick is that the static cells are always available in [super tableView:tableView cellForRowAtIndexPath:newIndexPath] - so use that for permanent storage of your static tableview cells. Then adjust the number of rows as needed, and map the rows correctly (ie, get the row of the stored cell that corresponds to the cell you want displayed) in your tableview delegate's cellForRowAtIndexPath:.
The updateSettingsDisplay method can be called on your tableview controller by any class that retains it. If the tableview controller is not visible when it is called, it will just update the state and wait until next time it becomes visible to change the display.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.section == 1) { //For example
return 0
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.section == 1) {
cell.hidden = true
}
}
you might want to look at this
https://github.com/xelvenone/StaticDataTableViewController/
usage
self.hideSectionsWithHiddenRows = YES; //YES, NO
[self cell:self.outletToMyStaticCell1 setHidden:hide];
[self cell:self.outletToMyStaticCell2 setHidden:hide];
[self reloadDataAnimated:YES];
FYI (would comment if I could), if you are overriding a Grouped static UITableView from the Storyboard, then you'll also need to override the Header & Footer views & heights for each section you are modifying/hiding.
I also needed to set the return value for the "GetHeight" methods to something other than zero - implying that a zero value means something else (autolayout, or "not set" or the like).
The below code is in Xamarin iOS C#, but translates directly to the appropriate Obj-C methods:
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (section == 0)
{
headerView = new UIView(new CGRect(0,0,0,0));
}
}
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
if (section == 0)
{
return 1f;
}
return base.GetHeightForHeader(tableView, section);
}
public override void WillDisplayFooterView(UITableView tableView, UIView footerView, nint section)
{
if (section == 0)
{
footerView = new UIView(new CGRect(0, 0, 0, 0));
}
}
public override nfloat GetHeightForFooter(UITableView tableView, nint section)
{
if (section == 0)
{
return 1f;
}
return base.GetHeightForFooter(tableView, section);
}
Just calculate the numberOfRows(inSection) to remove some rows you don't want
You need to put beginUpdates before the delete call and endUpdates after:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:r inSection:s]]];
[tableView endUpdates];
In response to your comment:
- (NSInteger)tableView:(UITableView)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger ret;
switch (section) {
case 0:
// sectionZeroRows doesnt have to be a
// property (an attribute works just fine), but it
// helps with explaining this example.
ret = self.sectionZeroRows;
break;
// ...
}
return ret;
}
- (void)someMethod {
--self.sectionZeroRows;
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]];
[tableView endUpdates];
}
// just authed with facebook, and I want to delete the first 3 rows of my 4 row table that are no longer necessary
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
if(wantsFacebook)
return 1;
return 4;
}
After committing the updates, the tableView will call the numberOfRowsInSection delegate to get the updated number of rows and it MUST be the correct sum/difference between the rows when you started the insert/delete and the rows when you ended the insert/delete

Use two different cell identifier on same table view

I'm trying to get rid of a cached UITableView cell. I have two sections. The first cell of the second section has the same "appearance" as the first cell of the first section. With appearance I mean the cell height (multiline cells).
I tried to use different identifiers, but that didn't worked. Here is the code:
NSString *identifier;
if (thisViewMode) {
identifier = #"thisViewMode";
} else if ((indexPath.section == 1) && thatViewMode) {
identifier = #"thatViewMode";
} else {
identifier = #"CellIdentifier";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [self CreateMultilinesCell:identifier];
}
thatViewMode is only called once, which isn't right because I have more cells in my second section. If I change the content of the first cell in the first section, the height of the first cell in the second section also changes too. Normally each cell should have it's own cell height, but that isn't the case here.
Is there a problem that I can't use different cell identifiers on the same table view?
I forgot to also adapt heightForRowAtIndexPath so that I make a differentiation between the sections.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
// do something
} else {
// do something different
}
return ...
}