Starting my quest into ios development but I have a problem, below.
The plan:
The result:
You need to set height of your custom cell using tableview delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 150.0;
}else if (indexPath.row == 1){
return 300.0;
}
return 0.0;
}
as well as u need to set your custom cell cliptobound property to yes in datasource method
- (CGFloat)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customcell *cell = (customcell *)[tableView dequeResusableCellWithIdentifier:#"newcell" forIndexPath:indexPath];
cell.cliptobound = YES;
return cell;
}
Your tableview needs to know the height of your custom cell, use the following tableview delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 100.0;
}
return 200.0;
}
Your cells are getting reused without the content being reset in cellForRowAtIndexPath. In that method you need to blank out the cell content before putting more content in it.
Related
I am calculating height of Cell programatically in heightForRowAtIndexPath-
My complete method is:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
if([cell isKindOfClass:[Custom class]])
{
return 100;
}
else
{
return 20;
}
}
My app takes 3-5 sec to Show the ViewController. Although cellForRowAtIndexPath is called and I can show the logs in Console.
But My ViewController not loaded.
When just return the height using this code app works fine:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
I don't know that is the issue with this line:
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
What I am missing in this issue?
You have some logic in the cellForRowAtIndexPath: method that determines what type of cell that row should be. Add that logic into the heightForRowAtIndexPath: method to determine the what the height of each row should be.
Your this line is wrong
id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];
it will force your cell to reuse.
Use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell=[tableView cellForRowAtIndexPath:indexPath];
if([cell isKindOfClass:[CustomCellClass class]])
{
return 100;
}
else
{
return 20;
}
}
I am unsure if I'm setting up the UITableView the right way when using multiple custom cell views. I seem to have my table working however, i think the reuse of cells makes the first cells you see once you load the view the same size as the very first cell which should be the only cell thats double in size. Once I scroll passed them and back up they go to the size thats set for them.
Is there a right way of doing this? I've looked through other forms and tried their ways but this is the closed I've gotten it to work with just that one flaw.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"profileCell";
static NSString *CellIdentifier2 = #"profileCells";
if (indexPath.section == 0) {
PFProfileTableCellView * cell = (PFProfileTableCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
//only one row in this section
return cell;
} else{
PFWallPostTableViewCell * cell = (PFWallPostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
return cell;
}
}
Use this UITableView delegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; and return row height according to the sections of your tableview like the example below
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return 100.0;
}
return 50.0;
}
How do you hide a static cell?
I would like to hide and static cell if an image does not exist.
I tried:
imageCell.hidden = YES; //did not work
I have seen answers suggesting to change datasource or use:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;// will hide all cell
}
But I couldnt find a way to do this with a specific view cell.
what I want to achieve:
if(image==nil){
//hide imageCell
}
Now here is the catch , the image is downloaded asynchronously, so deleguate methods might be called before the attempted downlaod.
Do the following :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2 && !myImageIsLoaded)
return 0; // Will hide just the third row of your table if myImageIsLoaded is false
return 44;
}
And you can use the following to animate all whenever you want (e.g. each time an image as loaded) :
[myTable beginUpdate];
[myTable endUpdate];
If your cells are static so it should work. Otherwise you could encounter some problems.
If you want literally hide the cell that image does not exist, you can try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
return [cell imageView] ? 44.0f : 0.0f;
}
A very simple question...
can we change the height of only current row(the row which is clicked) in UItableView?
Note: I dont want any affect on size of remaining rows.
Yes you can. You need a variable that keeps the last selected row. For ex.:
#property (nonatomic, assign) NSInteger selectedRow;
...
Then implement the method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.selectedRow) {
return 100.;
}
return 44.;
}
and then, update the method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(self.selectedRow == indexPath.row)
self.selectedRow = -1;
else
self.selectedRow = indexPath.row;
//The magic that will call height for row and animate the change in the height
[tableView beginUpdates];
[tableView endUpdates];
}
NOTE: Initialise your self.selectedRow with -1 value in the beginning as the default is 0.
You can use heightForRowAtIndexPath for setting height for a row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
You can place a check for NSIndexPath and return height for that particular indexpath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == rowtochangeheight) {
return 60;
}
else
{
return 44;
}
}
Yes you can do, But you need to save indexpath of selected row and you must intialize selectedRow variable by negative value.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexpath.row;
[tblView reloadData];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == selectedRow)
return 150.0f;
return 30;
}
I am new at iOS development.
I want to check if my table is empty. If it is, I want to :
Increase height of the first row and display "No new messages!"
OR
Get rid of the table and just display "No new messages" in the center of the page.
How would I do that?
(Let's assume you have an array from which you want to populate the table view. This is a pretty standard way for populating table views. Let's call this theoretical array dataArr.)
In your data source:
- (NSUInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSUInteger)section
{
if ([dataArr count] == 0) {
// empty table
return 1;
}
return [dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"someCellID"];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"someCellID"] autorelease];
if ([dataArr count] == 0) {
// empty table
cell.textLabel.text = #"No new messages";
} else {
// configure as normally
}
return cell;
}
In your delegate:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)ip
{
if ([dataArr count] == 0) {
// empty table
return 88.0f; // 2 times the normal height
}
// else return the normal height:
return 44.0f;
}
I suppose you have an array of messages you are attempting to display
You can define custom height for cells with the function
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0)
return 80;
else
return 44;
}
and then, when you are creating the cells: (make sure the "no new messages" cell has a different cell identifier than the regular cells, so that your cell re-use won't mess things up)
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0) {
// create the "No New Messages" cell
}
else {
// create regular cells
}
}
This should help:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView numberOfRowsInSection:1] == 0) {
return 200;//some other height
}else{
return 44;
}
}
Basically you want to check against your data source to see if you have no messages in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([data count] == 0) return 100;
else return 44;
}