It is my fervent dream to have a UITableView with different row sizes, determined programmatically. I have read the UITableViewDataSource documentation, and I implement these methods:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
which, we can see here:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (CGFloat) 200.0f;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* test = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil ];
test.textLabel.text = #"TEST";
CGRect ffframe = test.frame;
ffframe.size.height *= 200.0f;
test.frame = ffframe;
return test;
}
These are just trying to make a simple table, with one row, and a height of 200.0. I try to return the height in the delegate method, and set it explicitly. Neither works.
I try trapping the heightForRow method in the debugger, it seems to never be called.
MY table rows are always the size I set in interface builder.
I have the datasource hooked up right, otherwise it would not get the rows and textLabel right.
This is a delegate method so ensure that you have
self.tableview.delegate = self;
in viewDidLoad or else have the tableview delegate property in the xib linked up to the controller.
Careful, this method is a tableview delegate not dataSource method.
Related
Hello I'm beginner in iOS In one of my activity I want to reload table data ...then I have applied this code .....
-(void)buttonpress
{
for(int i=0;i<buttonarr.count;i++)
{
NSString *butt=[NSString stringWithFormat:#"%#",[buttonarr objectAtIndex:i]];
for(int j=0;j<LabelTag.count;j++)
{
NSString *labelbutt=[NSString stringWithFormat:#"%#",[LabelTag objectAtIndex:j]];
if([butt isEqualToString:labelbutt])
{
NSIndexPath *indexPath1 =[NSIndexPath indexPathForRow:j inSection:0];
NSLog(#"indexPath %#",indexPath1);
UITableViewCell *mycel=[self->table_AdviseTestDetails cellForRowAtIndexPath:indexPath1];
NSLog(#"cell %#",mycel);
UILabel *mybutton=(UILabel*)[mycel viewWithTag:100+j];
// mybutton.selected=YES;
NSLog(#"label %#",mybutton.text);
r=r+[mybutton.text floatValue];
NSLog(#"r %f",r);
}
}
}
range=1;
[table_AdviseTestDetails reloadData];
netcharges.text=0;
z1=0;
NSLog(#"label arr %#",LabelArr);
for(int i=0;i<LabelArr.count;i++)
{
NSString *y= [NSString stringWithFormat:#"%#",[[LabelArr objectAtIndex:i] valueForKey:#"value"]];
z1=z1+[y integerValue];
netcharges.text=[NSString stringWithFormat:#"%.2f",z1];
NSLog(#"net %#",netcharges.text);
//balance.text=[NSString stringWithFormat:#"%.2f",z];
}
netcharges.text=[NSString stringWithFormat:#"%.2f",[netcharges.text floatValue]-r];
NSLog(#"net %#",netcharges.text);
point.text=[NSString stringWithFormat:#"%.f",(floor([netcharges.text integerValue]/[[[Point_Result objectAtIndex:0]valueForKey:#"Value"] integerValue]))*[[[Point_Result objectAtIndex:0]valueForKey:#"Points"] integerValue]];
balance.text=netcharges.text;
NSLog(#"net %#",balance.text);
}
This is my code please ignore all code think is there [table_AdviseTestDetails reloadData];
when we reload table then my controller go in table datasource ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and after this controller come below line of this
[table_AdviseTestDetails reloadData];
and after this controller goes in ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have problem there .....I want when we reload table then first all datasource and delegate of UITableview run and then run other thing.....
So IN my above code ....I want fist reload table data use this
[table_AdviseTestDetails reloadData];
then run all delegate and datasource of table.....
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and controller come after this below of [table_AdviseTestDetails reloadData];
this line .....how can do this ..please solve this problem ......thank in advance
these is the general sequence i am not including all the methods just required ones are here
#pragma mark
#pragma mark - Table view data source
1) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3) -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
4) -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark
#pragma mark - Table view delegate
1) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I have made a table with two types of custom cells using interface builder:
Everything works fine, except lower cell's height is same as cell above it. It has everything i put - UIImageView etc...
So I was thinking if it is not possible to change cell height when table style is grouped?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1) {
return 100;
}
return 44;
}
We can use UITableview Delegate Methods :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1)
{
return 60;
}
else
{
return 44;
}
}
You have to use this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75.0f;
}
it is use full for me.
I created a custom UITableViewCell in my app and the default indent when the Delete button is present is not happening, below is a screenshot of my settings. Anyone have any idea as to what I'm doing wrong?
Also, here's a shot of the IB properties:
should happen by default, but in your UITableViewDelegate override
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
make sure you aren't doing anything strange in:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
and that you are setting the cell's editingAccessoryType to UITableViewCellEditingStyleDelete or UITableViewCellEditingStyleInsert
and that you aren't returning No from :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I have an UITableView which includes a list of UITableViewCells. And I set the heights of UITableViewCells in method (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.
However, I need to increase the height of UITableViewCell when it is selected. For example, the height of UITableViewCell needs to increase 100 pixels when the cell is selected, does anyone know how to do it?
Store selected index in your controller.
Return cell's height depending on that index:
CGFloat height = 30.0f;
...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.row == selectedIndex ? height+100 : height;
}
Refresh tableview geometry in didSelectRowAtIndexPath method (empty block between beginUpdates and endUpdates does the trick):
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
index = indexPath.row;
[tableView beginUpdates];
[tableView endUpdates];
}
Make your delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return the (new) correct height and make the table view reload the given path.
I am new to iphone development.I am displaying a xml parsed contents in a grouped tableView.I want disable the click event on it(i should not be able to click it at all) .Since it is grouped table , it contains two tables and i want to disable the first table only and not the second table.How can i achieve it?Please help me out.Thanks.
If you don't want the user to be able to click on a table view, just use this code:
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
Use Uitable View delegates and data Sources
//#endif
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning num sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning num rows");
return [copyListOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}