Reputation: 1888
I have a custom tablecell with three labels. If a label is empty, i hide it. In my Objective-C Version
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailCell *cell = (DetailCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat height = 84.0;
if (IsEmpty(cell.lblArt.text))
height -= 24;
if (IsEmpty(cell.lblTelefon.text))
height -= 24;
return height;
}
i have this result:
In heightForRowAtIndexPath i must access the cell to calculate the height based on the hidden labels. This is one of my tries in Swift:
override func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
let cell:DetailCell = self.tableView.cellForRowAtIndexPath(indexPath) as DetailCell // ????????
var height:CGFloat = 84.0;
if cell.lblArt.text.isEmpty{
height -= 24
}
if cell.lblTelefon.text.isEmpty{
height -= 24 }
return height;
}
But i can't get the cell, here starts a never-ending loop. :-(
How can i do that in Swift?
Upvotes: 0
Views: 1320
Reputation: 77651
At the moment you are inspecting the cell to try and find if the telephone number exists. This is the wrong way to go about this completely.
In your code for tableview:cellForRowAtIndexPath:
you will have something like this..
- (UItableViewCell*)tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// do some stuff
// get a telephone number from the data
NSString *theTelephoneNumber = //get something from core data
// set the text in the label in the cell
cell.lblTelefon.text = theTelephoneNumber;
return cell;
}
So in your method for height
you should be doing this...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *theTelephoneNumber = //get something from core data
// this is the same as the above method
CGFloat height = 84.0;
if ([theTelephoneNumber isEqualToString:@""]) {
height -= 24;
}
return height;
}
Don't go to the view to get data. Go to the data to get data.
Upvotes: 0