I have code
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 9) {
height = 124.0f;
}
return height;
}
but cellForRowAtIndexPath returns 44.0f for indexPath.row == 9
Why is not returning 124?
Related
I have two section in UITableviewCell. I want hide that two section.
this is my code.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return CGFLOAT_MIN;
}
else
{
return 32.0f;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [storename2 count];
}
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return nil;
}
else
{
return [storename2 objectAtIndex:section];
}
}
In Viewdidload.
- (void)viewDidLoad
{
[super viewDidLoad];
self->CartTableview.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
But its only Hide First Section,
show below image i want like this (When Card Is Empty Hide Sections and when i add products to the card show all(two)section.)
help me,
show 1st image.
when i add product to card two section will show and when i remove the product all section will hidden.
Thanks in Advance.
Just return 0 in heightForHeaderInSection tableview delegate method...
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// Specific section
if (section == 0)
return 0.0f;
// all sections
return 0;
}
I think you need change in bellow method
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
And Remove this method
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return nil;
}
else
{
return [storename2 objectAtIndex:section];
}
}
This will hide all the section header in your tableview.
Also don't forget to set UITableview Delegate & datasource.
- (void) viewDidLoad {
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// Specific section
if (section == 0)
return 0.0f;
// all sections
return 0;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{ // Specific section
if (section == 0) {
return nil;
} else {
// all sections
// return some string here ...
return nil;
}
}
Use switch-case to handle multiple section height.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let headerHeight: CGFloat
switch section {
case 0:
// first section for test - set value for height
headerHeight = 0
default:
// other Sections - set value for height
headerHeight = 0
}
return headerHeight
}
I have a UITableView that serves as a menu (user picks a row and fires a segue). Currently my code looks like:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self isAuthorized]) {
if ([self isAdmin]) {
return 7;
}
return 5;
} else {
return 4;
}
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self isAuthorized]) {
if ([self isAdmin]) {
if (indexPath.row == 0) {
// code 1
} else (indexPath.row == 1) { {
// code 2
} // etc...
} else {
if (indexPath.row == 0) {
// code 1
} else (indexPath.row == 1) { {
// code 2
} // etc...
}
} else {
if (indexPath.row == 0) {
// code 1
} else (indexPath.row == 1) { {
// code 2
} // etc...
}
}
is it possible to tie action initiated by cell with a cell, so didSelectRowAtIndexPath looks simplier?
You could create your custom UITableViewCell classes and try doing the following:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id cell = [tableView cellForRowAtIndexPath: indexPath];
if ([cell isKindOfClass: [AdminCellClass class]])
{
//code here
}
else if
...
}
I'm expanding my cell via:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRow && indexPath.row == selectedRow.row) {
tableView.scrollEnabled = NO;
return 480;
}
return 250;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
CGFloat height = 250.0f;//cell height
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES];
}
It works fine, whenever I select a cell, it do expands, but I couldn't figure out how to bring it back to its original height. Any thoughts?
Try this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRow && indexPath.row == selectedRow.row) {
tableView.scrollEnabled = NO;
return 480;
}
tableView.scrollEnabled = YES;
return 250;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 250.0f;
if (selectedRow && indexPath.row == selectedRow.row) {
NSIndexPath *index = [NSIndexPath indexPathForRow:selectedRow.row inSection:selectedRow.section];
selectedRow = nil;
[tableView reloadRowsAtIndexPaths:#[index] withRowAnimation:UITableViewRowAnimationNone];
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
[tableView setContentOffset:CGPointMake(0, 0 )animated:NO];
} else {
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:NO];
}
}
Try This :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedRow.row == indexPath.row)
selectedRow = nil;
else
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
[tableView reloadData];
}
I would suggest you to add a custom property like isExpanded in your tableView datasource object and switch that property based on selection and return height according to this value.
For ex: In your heightForRowAtIndexPath method, you could have somthing like this.
if(yourCustomCell.isExpanded == true){
return 480;
}
else{
return 250;
}
In didSelectRowAtIndexPath method, just set or unset this property.
yourCustomCell.isExpanded = !yourCustomCell.isExpanded
[tableView reloadData]; //Which will call heightForRow and then assign new height based on the property value.
Now if you dont have a custom cell object, then I think you can make use of cell's isSelected property.
I haven't tried these, but I guess this should work.
I have a viewcontroller in that i want to show 3 tableviews(because the content and the table properties are different). How do i add these delegate methodes for 3 tables in one viewcontroller?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
EDIT
So what will i do if i want add a uislider to one table row using custom cell and when i slide the value i want to change the display brightness?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == _displayThemes) {
return 1;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView==_displayThemes) {
return 1;
} else {
return 5;
}
}
- (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];
}
if (tableView == _displayThemes) {
cell.textLabel.text = [displaytheme objectAtIndex:indexPath.row];
return cell;
} else {
cell.textLabel.text = [fontlist objectAtIndex:indexPath.row];
return cell;
}
}
- (IBAction)fontButton:(id)sender {
_fontList = [[UITableView alloc]init];
[self.view addSubview:_fontList];
[UIView animateWithDuration:1.5
delay:0
options: UIViewAnimationOptionTransitionCurlUp
animations:^{
_fontList.fram e= CGRectMake(0,800,320,200);
}
completion:^(BOOL finished){
_fontList.frame = CGRectMake(0,280,320,200);
}];
[_fontList reloadData];
}
- (IBAction)button:(id)sender {
_displayThemes = [[UITableView alloc]init];
[self.view addSubview:_displayThemes];
[UIView animateWithDuration:1.5
delay:0
options: UIViewAnimationOptionTransitionCurlUp
animations:^{
_displayThemes.frame=CGRectMake(0,800,320,200);
}
completion:^(BOOL finished){
_displayThemes.frame=CGRectMake(0,280,320,200);
}];
}
It will be the same as you do it with one table view, but you should check which tableview is currently using.
myTableView1.dataSource = self;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == myTableView1) {
// your code 1
}
else
if (tableView == myTableView2) {
// your code 2
}
else
if (tableView == myTableView3) {
// your code 3
}
}
Edit:
About brightness:
How to change brightness in iOS 5 app?
And about UISlider it has minimunValue and maximumValue properties.
- (void) sliderChanged:(UISlider*)sender{
UISlider *slider = (UISlider*)sender;
[[UIScreen mainScreen] setBrightness:slider.value];
}
Edit:
slider.tag = 1;
[cell addSubview:slider];
...
// when you need..
indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion];
UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
You always get a reference and can always check for which tableView delegate or dataSource method is called.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.tableView1)
{
return 1;
}
if (tableView == self.tableView2)
{
return 1;
}
if (tableView == self.tableView3)
{
return 1;
}
}
You don't gain anything by using same identifier for all tables. Use something like:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (tableView == self.tableView1)
{
static NSString *CellIdentifier1 = #"cellForTable1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
cell.textLabel.text = [NSString stringWithFormat: #"table1: %d.%d", indexPath.section, indexPath.row];
return cell;
}
if (tableView == self.tableView2)
{
static NSString *CellIdentifier2 = #"cellForTable2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
cell.textLabel.text = [NSString stringWithFormat: #"table2: %d.%d", indexPath.section, indexPath.row];
return cell;
}
if (tableView == self.tableView1)
{
static NSString *CellIdentifier3 = #"cellForTable3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
}
cell.textLabel.text = [NSString stringWithFormat: #"table3: %d.%d", indexPath.section, indexPath.row];
return cell;
}
}
//add tag in tableView .
myTable1.tag = 200;
myTable2.tag = 201;
myTable3.tag = 202;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 200)
{
return 1;
}
if (tableView.tag == 201)
{
return 1;
}
if (tableView.tag == 202)
{
return 1;
}
}
You can manage multiple tableView in a single ViewController by
writing below code inside UItableViewDelegate and UItableViewDatasource.
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableView1
{
// place your code here
}
else if tableView == tableView2 {
// place your code here
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tableView1
{
// place your code here
}
else if tableView == tableView2 {
// place your code here
}
else {
return 0
}
}
// You can set a different size of your tableView using below lines of code
if tableView == tableView1{
return 50
}
else{
return 40
}
None of the previous worked for me, I come up with the following solution:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if((tableView1 != nil) && (tableView == tableView1)) {
return Items1.count
}
else if((tableView2 != nil) && (tableView == tableView2)) {
return Items2.count
}
else if((tableView3 != nil) && (tableView == tableView3)) {
return Items3.count
}
else {
return 0
}
}
I have a table with static cells. For one cell I want to change its height depending on the label's height (inside that cell) and at the same time leave all other cells height intact. How can I get current cell's height? Or maybe there is better approach?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath section] == 2) {
return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height;
} else {
return ...; // what should go here, so the cell doesn't change its height?
}
}
You can call:
[super tableView:tableView heightForRowAtIndexPath:indexPath]
in the else block so you don't have to worry if ever you changed the default height.
You can get/set default height tableView.rowHeight, or you can store your height before changing cell height, so you can get default height from some variable;
Please do this one
if ([indexPath section] == 2)
{
if(indexPath.row == 1)
return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height;
else
tableView.rowHeight
}
#talnicolas great answer, here's for Swift 3:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 2 {
let easy = self.myLabel.frame
return easy.origin.y *2 + easy.size.height
} else {
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
You, maybe, want to calculate height of label in constrained width. In this case you can create method like that:
- (CGFloat)textHeightOfMyLabelText {
CGFloat textHeight = [self.myLabel.text sizeWithFont:self.myLabel.font constrainedToSize:LABEL_MAX_SIZE lineBreakMode:self.myLabel.lineBreakMode].height;
return textHeight;
}
and use result in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.
Don't forget to add margin value of you label.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == youSectionNumber)
{
if (indexPath.row == numberOfrow)
{
return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height;
}
}
return 44; // it is default height of cell;
}