I have the following code:
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
TestCell* cell = (ConvoreCell *) [tableView cellForRowAtIndexPath:indexPath];
CGSize textSize = [[cell text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)];
return 80;
}
It crashes on the cellForRowAtIndexPath, why is this? The log doesn't tell me anything. Here's my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ConvoreCell";
AsyncImageView *asyncImageView = nil;
TestCell * cell = (TestCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TestCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ConvoreCell *) currentObject;
break;
}
}
asyncImageView = [[[AsyncImageView alloc] init] autorelease];
asyncImageView.imageview = cell.icon;
}
else {
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
}
NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];
NSString *picURL = [[cellValue objectForKey:#"user"] objectForKey:#"img"];
if ([picURL isEqualToString:#"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
picURL = #"https://test.com/media/images/eric.png";
[asyncImageView loadImageFromURL:[NSURL URLWithString:picURL]];
cell.title.text = [cellValue objectForKey:#"message"];
cell.info.text = [NSString stringWithFormat:#"%# %# days ago", [[cellValue objectForKey:#"user"] objectForKey:#"username"], [cellValue objectForKey:#"date_created"] ];
return cell;
}
All I want to do is to resize the height of the cell according to the cell.title.text....
It's wrong to call cellForRowAtIndexPath: manually. You have to store those strings separately in array and obtain them from there.
A rule of thumb: don't call framework callbacks manually.
Related
i have two tableviews in my view. i have issue in implementing the delegate methods. Can anyone help me on this. Here the problem is, this method requires a return value of type UITableViewCell right. what should i return?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
return cell;
}
if (tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"ContactListCustomCell";
ContactListCustomCell *cell = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
return cell;
}
}
Use this edited code. It will also get rid you of any warning regarding return cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifier_contact = #"ContactListCustomCell";
UITableViewCell *cell;
if(tableView==ContactTableview)
{
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
}
if (tableView==ContactTableViewLabel)
{
ContactListCustomCell *cell1 = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell1 = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell1.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell1.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
cell = (UITableViewCell*) cell1;
}
return cell;
}
There is no need to cast ContactListCustomCell as a UITableViewCell. You can use ContactListCustomCell as a subclass of UITableViewCell. You can update your code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
.
.
}
else if(tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"yourCellNibName" owner:self options:nil];
cell = self.yourCellProperty;
self.yourCellProperty = nil;
}
.
.
}
return cell;
}
GoodLuck !!!
I am new to iphone. my cutomcell label value is disappearing when I scroll the table view.
Again it appears when I click on that cell.
Can anyone help me?
Thanks in Advance.
//table view in view controller created in xib
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:#"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSString *offerStr= [NSString stringWithFormat:#"%.2f",[[productItemDit objectForKey:#"offer"] floatValue]];
NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:#" "] stringByAppendingString:offerStr];
NSLog(#"%#",fullCostStr);
cell.itemCostLbl.text=fullCostStr;
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
NSLog(#"searchdit:%#",searchProductItemDit);
cell.itemNameLbl.text= [searchProductItemDit objectForKey:#"name"];
self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
} else {
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSLog(#"dit:%#",productItemDit);
cell.itemNameLbl.text=[productItemDit objectForKey:#"name"];
}
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:#"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:#"profp.jpg"];
return cell;
}
Solved by doing like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:#"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
cell.itemNameLbl.text= [[searchProductListArry objectAtIndex:indexPath.row] objectForKey:#"name"];
} else {
cell.itemNameLbl.text=[[productListArry objectAtIndex:indexPath.row] objectForKey:#"name"];
}
cell.itemCostLbl.text=[NSString stringWithFormat:#"%# %.2f", currencyCodeStr , [[[productListArry objectAtIndex:indexPath.row] objectForKey:#"offer"] floatValue]];
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:#"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:#"profp.jpg"];
return cell;
}
As you haven't post much of the information this is my guess.when you scroll table view it internally calls the reloadData method . You need to use reuse Identifier to preserve the state of the cell. You can initialize the cell in cellForRowAtIndexPath like :
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// or cell initializing logic here for the custom cell
}
in case of custom cell add reuse identifier in the xib and use it in your cellForRowAtIndexPath method
I have created a custom cell that contain a Label but when I added that in my Table View than it does not display, my view controller is not TableViewController I have seted Table View using IB and seted its delegate and datasource correctly.
I am doing this by:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
return theDataObject.myAudio.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.audioName.text = filename;
return cell;
}
First check how many row set in tableView:numberOfRowsInSection method
After set the custom cell like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AudioInfoCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AudioInfoCell *) currentObject;
break;
}
}
}
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
cell.audioName.text = filename;
return cell;
}
Two things to check:
Make sure the reuse identifier matches what is in Interface Builder
Make sure your numberOfRowsInSection and numberOfSectionsInTableView
return the right numbers
Also - I don't use:
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
for instantiating the cell. That should be taken care of in the AudioCell TableViewCell subclass you have implemented. An example of how I implement this kind of custom cell is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"FlightPickerCell";
FlightPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tripIDLabel.text = #"Some TripID";
cell.hospitalLabel.text = #"Some Hospital";
cell.cityLabel.text = #"Some City";
return cell;
}
I have two UILabel in side of UICell, which contains dynamic text so i need to resize its frame according to content, for which im using [string sizeWithFont] method to calculate the height of label view's frame inside of TableView heightForRowAtIndexPath to set the height of cell according to label height. Now what the problem is when i scroll my table, label inside of cell starts to shrink if i remove [label sizeToFit] method it doesn't shrink but from that my labels are getting overlapped which looks very messy. Where i am wrong please guide me..
here is my code for cellRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell");
BOOL ente = FALSE;
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
ente = TRUE;
break;
}
}
}
/*
[cell.title sizeToFit];
[cell.dataTime sizeToFit];
CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.title.frame = CGRectMake(50, 6, size1.width, size1.height);
CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.dataTime.frame = CGRectMake(50, 24, size2.width, size2.height);
*/
cell.title.text = [mainEventArray objectAtIndex:[indexPath row]];
cell.dataTime.text = [mainEventTimeArray objectAtIndex:[indexPath row]];
//if (ente) {
// NSLog(#"helllloo");
[cell.title sizeToFit];
[cell.dataTime sizeToFit];
//}
return cell;
}
and for heightForRowAtIndexPath method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
{
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
}
CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.title.frame = CGRectMake(0, 0, size1.width, size1.height);
CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.dataTime.frame = CGRectMake(0, 0, size2.width, size2.height);
//NSLog(#"%f", size1.height + size2.height + 20);
return size1.height + size2.height + 20;
//NSString *str = [heights_ objectAtIndex:[indexPath row]];
// return [str floatValue] ;
}
Instead of doing your own customization. Try with dynamic uitableviewcell height. You will find really usefull & comparatively easier than what you have done.
You can check it here : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full of explanation. Enjoy programming.
In your case you need to set the height of cell based on the label.
Check the link :
http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/
There is a function named
-(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
Use that function to calculate height as :
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat _height = [self getHeightForLabel:self.label.text font:[self.label font]];
return _height;
}
and do the same while creating and adding label in the cell.
I hope this will help.
NSString *reuseIdentifier = #"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
cell=nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;
}
Try This code.
label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = #"some long text";
[label sizeToFit];
NSLog(#"Label's frame is: %#", NSStringFromCGRect(label.frame));
For reference : click here
I have created a UISplitViewApplication as my new project. In the potrait mode I have a button that when clicked will drop down a UITableView. It looks something as the following:
When I clicked on one of the rows, this UITableView is dismissed. Then when I click again on the Groups button, the layout of the UITableView presented is now all messy:
However, when I scroll the TableView so that some of the rows are reloaded again, those that are reloaded are then formatted fine. Why is this and how do I fix this?
Here's my cell for row at index path:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCell *cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MyCell *) currentObject;
break;
}
}
cell.delegate = self;
// }
if ([posts count] > 0){
cell.star.hidden = YES;
[lazyImages addLazyImageForCell:cell withIndexPath:indexPath];
cell.title.text = [[posts objectAtIndex:indexPath.row] message];
cell.detailed.autoresizesSubviews = YES;
cell.detailed.text = [NSString stringWithFormat:#"%#", [[[posts objectAtIndex:indexPath.row] creator] username]];
if ([[[posts objectAtIndex:indexPath.row] embeds] count] != 0){
NSString * URL = [[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] url];
float height = [[(Embed * )[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] height] floatValue];
float width = [[(Embed * )[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] width] floatValue];
[cell.embed setFrame:CGRectMake(cell.detailed.frame.origin.x, cell.detailed.frame.origin.y + 15, width, height)];
cell.embed.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL]]];
}
if ([[[posts objectAtIndex:indexPath.row] stars] count] != 0){
cell.star.hidden = NO;
cell.star_creator.text = [(Login *)[[[[posts objectAtIndex:indexPath.row] stars] objectAtIndex:0] user] username];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText =[[posts objectAtIndex:indexPath.row] message];
UIFont *cellFont = [UIFont fontWithName:#"Arial" size:14.0];
CGSize constraintSize = CGSizeMake(600.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
float height = 0.0;
if ([[[posts objectAtIndex:indexPath.row] embeds] count] != 0)
height = [[[[[posts objectAtIndex:indexPath.row] embeds] objectAtIndex:0] height] floatValue];
if (labelSize.height + 20 + height < 48)
return 55;
else
return labelSize.height + height + 48;
}
Reposted from a previous comment: reload the visible cells in viewWillAppear.
I have been through this before, In your CellForRowAtIndex delegate and don't check if the cell equals nil just keep going.
Update
NSString *sIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sIdentifier];
// don't check if(cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sIdentifier] autorelease];