When I scroll UITableView the memory keeps on increasing and I get this sort of results when I run "Allocations".
I see their that VM:UITableViewLabel memory keeps on increasing and is persistent, Is their any way to I could remove this constant increase in memory
-(UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString *text = #"";
BOOL defaultStyle = YES;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (defaultCell == nil) {
defaultCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else{
NSLog(#"reusing the cell");
}
defaultCell.textLabel.textAlignment = NSTextAlignmentLeft;
defaultCell.userInteractionEnabled = YES;
defaultCell.textLabel.font = [UIFont systemFontOfSize:26.0f];
UIImageView *iv = defaultCell.imageView;
int totalSections = [self numberOfSectionsInTableView:tableView];
NSInteger computedSection = indexPath.section;
if (defaultStyle)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.backgroundColor = [UIColor whiteColor];
defaultCell.textLabel.textColor = [UIColor blackColor];
}
else if (computedSection == 0)
{
const int totalRows = [self tableView:self.tableView numberOfRowsInSection:indexPath.section];
if (indexPath.row == 0) {
text = #"Style1";
defaultStyle = NO;
if (self.style1 == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
iv.image = [UIImage imageNamed: #"Style1.png"];
}
if(indexPath.row == totalRows - 2){
// Categories
text = #"Style2";
iv.image = [UIImage imageNamed: #"Style2.png"];
defaultStyle = NO;
if ( self.style2 == nil) {
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
} else {
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor blackColor];
}
}
else if (indexPath.row == totalRows - 1){
// Search
text = #"Style3";
iv.image = [UIImage imageNamed: #"Style3.png"];
}
}
else if (computedSection == 1)
{
MyCustomTableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:kCustomTableCellIdentifier];
if ( cell == nil ) {
cell = [CustomTableViewCell loadFromNibNamed:#"CustomTableViewCell"];
}
cell.titleLabel = #"custom cell";
cell.openImage =[UIImage imageNamed: #"custom.png"]
return cell;
}
else if (indexPath.section == totalSections - 1)
{
if (indexPath.row == 0)
{
text = #"Account";
defaultStyle = NO;
if (self.hasAccount == nil)
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleNone;
defaultCell.textLabel.textColor = [UIColor grayColor];
}
else
{
defaultCell.selectionStyle = UITableViewCellSelectionStyleBlue;
defaultCell.textLabel.textColor = [UIColor whiteColor];
}
iv.image = [UIImage imageNamed: #"Account.png"];
}
}
defaultCell.textLabel.text = text;
return defaultCell;
}
I have copied your code into a new project, and made some tests. It has never used more than 10 Mb memory on a real device, so there can be 2 cases.
Calling a method which leaks. At this point you call only one method in cellForRow because: BOOL defaultStyle = YES; This method is:
int totalSections = [self numberOfSectionsInTableView:tableView];
Could you post also this method please?
UITableViewCell subclass leaks while reusing. This isn't an option now since you are using UITableViewCell.
Small note #1: remove the autorelease if the project is in ARC.
Small note #2: Use
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
instead of:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (defaultCell == nil) {
defaultCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else{
NSLog(#"reusing the cell");
}
and you don't need to check and alloc new cells manually.
Thanks all for your help, the following solution worked for me
1.
-(void) viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kDefaultCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:#"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:kCustomTableCellIdentifier];
}
as told by #Péter Kovács
2.
Used indexPath method for both cells
static NSString *CellIdentifier = kDefaultCellIdentifier;
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
static NSString *CellIdentifier = kCustomTableViewCellIdentifer;
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
3.
Main problem I was using the defaultCell globally, it dequeued the defaultCell also for the CustomTableViewCell, so in the case CustomTableViewCell block was triggered it dequeued two cells. Dequeue ing respective cell based on their rows solved the problem.
I think you are not reusing the cells used in your table view
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
} else {
// reset your cell values here
}
Use this inside your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Related
I set uitableview's cell as following
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
[cell addSubview:bigphoto];
}
else {
cell.backgroundColor = [UIColor blackColor];
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 320;
}
return 220;
}
I want the first cell(indexPath.row = 0) is different from others, the first cell is set a bigphoto and its background color is red(with its UI Design), others are myphoto.png with black color background(with the UI Desigin of DetailCell), but the code runs with the wrong result,
the first cell(indexPath.row = 0) is right, but the indexPath.row = 3 or 6 or 9.. are the same as indexPath.row = 0, not like indexPath.row = 1 or 2 or 4...
so how can I fixed this? thanks
The cell is being reused, you should use different cell identifiers for cells with different representation. This should work:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
DetailCell *cell = nil;
if (indexPath.row == 0) {
cellIdentifier = #"Cell0";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bighoto.tag = 1;
[cell addSubview:bigphoto];
}
UIImageView *bigphoto = (UIImageView *)[cell viewWithTag:1];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
}
else {
cellIdentifier = #"Cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor blackColor];
}
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
This is because the first cell is "reused." Since you are using these cells for two separate purposes, it would be much cleaner to have a prototype cell in the storyboard that you use for just the first row, and then otherwise reuse cells for the rest of the rows.
In your case best option is to use the static tabelViewCells. Please check on this link.
SOLUTION
Just read the answer of #Kjuly
Thanks alot
QUESTION
I used tableView with sections, each section has 4 rows, the first row it must show image from website which I used HJCache class to cache the image and avoid leaking/memory issues.
now, this code it works well and while I am scrolling fast it doesn't leak or make memory issue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
}
But when I try to configure other rows it leaked and while scrolling the application make memory issue and be very slowly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (indexPath.row == 1) {
// configure row 1
}
if (indexPath.row == 2) {
// configure row 2
}
// etc for the others ..
return cell;
}
Where is the problem, Thanks..
UPDATE
This code doesn't work well, it add the subview in other row while scrolling
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
}
For your other cells, you need to reuse the cell either:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
EDIT for Updated Question:
You need to know how "cell reusing" works. As for your updated code, it said only row 1 needs the subviews like image, right? So you need add it outside of the if (cell == nil){} snippet, like the code below:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
Note: It's better to create a new cell instance like what you did for row 0, cause it's only need to be created once.
try this ...
man you are doing 2 mistakes here
not checking for dequeued cells.
after allocating the cell , you are not releasing it's memory.
i suggest this code for you .
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cellID";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
}
don't forget to autorelease cells .
remove all add subviews, then add new subviews
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
I have 3 sections in a table view and only using the middle section, section 2, to show various cells. Sections 1 and 3 only show one cell and I am making them unclickable since I want to display buttons and text on them. I made them and it was working fine until I made sections 1 and 3 userInteractionEnabled=NO.
Code: I know I can make this Object Oriented, and it was, but once this problem came up I tried to make it differently but it is still the same.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
cell.selectedBackgroundView = selectedBackgroundView;
if(cell==nil) { NSLog(#"Cell is nil");}
}
if(indexPath.section == 0)
{
cell.textLabel.text = nil;
cell.accessoryView = nil;
cell.detailTextLabel.text = nil;
dosageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
amountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dosageButton addTarget:self action:#selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
[amountButton addTarget:self action:#selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dosageButton];
[self.view addSubview:amountButton];
cell.userInteractionEnabled = NO;
return cell;
}
else if (indexPath.section == 1)
{
if (self.nameMutable.count != 0 )
{
cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#",[self.priceMutable objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chevron"]];
return cell;
}
else
{
//Empty for now. Waiting for data fetching to finish
return cell;
}
}
else if (indexPath.section == 2)
{
cell.userInteractionEnabled = NO;
cell.accessoryView = nil;
cell.detailTextLabel.text = nil;
return cell;
}
else
{
NSLog(#"Something went wrong");
return cell;
}
}
For some reason my table view cell in section 1 where it is supposed to be clickable the color changes to a dark grey and is not clickable anymore. Its usually cell 3 and cell 10. Also, when I scroll down and Section 0 is no longer visible and then I scroll back up and Section 0 is visible, some of the cells become non-clickable and the color of the text changes.
Also, how can I make a certain cell, inside section 1, have bigger height because the text is too long to display and it starts to display "..." or covers the detailTextLabel. Thanks in advance.
You have to remember that these cells are being reused or 'recycled' so if you're setting userInteractionEnabled=NO for an if statement you need to set it to userInteractionEnabled=YES in your else statement, or set it as YES before all your statements. You also want to make sure that you're adding any other subviews (buttons etc.) that are unique to certain index paths to cells that are newly created, where you would stick that piece of code inside your if(cell==nil) statement. Something like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
cell.selectedBackgroundView = selectedBackgroundView;
if(indexPath.section == 0) {
dosageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
amountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dosageButton addTarget:self action:#selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
[amountButton addTarget:self action:#selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dosageButton];
[self.view addSubview:amountButton];
}
if(cell==nil) { NSLog(#"Cell is nil");}
}
cell.userInteractionEnabled = YES;
cell.accessoryView = nil;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
if(indexPath.section == 0)
{
cell.userInteractionEnabled = NO;
}
else if (indexPath.section == 1)
{
if (self.nameMutable.count != 0 )
{
cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#",[self.priceMutable objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chevron"]];
}
else
{
//Empty for now. Waiting for data fetching to finish
}
}
else if (indexPath.section == 2)
{
cell.userInteractionEnabled = NO;
}
else
{
NSLog(#"Something went wrong");
}
return cell;
}
And if you want to change the height of certain index paths (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Docs delegate method.
I need to display the cell data. Actually if I debug the code I can see that the cell.label.text does have correct value, but nothing is getting displayed on the simulator. I have set the row count in numberOfRowsInSection method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
LogTableCell *cell = (LogTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
CGRect frame1 = [ self.view frame];
frame1.origin.x=22.0f;
frame1.origin.y=55.0f;
frame1.size.width = 298.0f;
frame1.size.height= 425.0f;
[self.view.superview setFrame:frame1];
int btnWidth = ((self.view.frame.size.width-16)/3);
if(cell == nil)
{
cell = [[LogTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
LogItem *lg = [[LogItem alloc]init];
lg = [arrLogs objectAtIndex:indexPath.row ];
NSLog(#"%d",indexPath.row);
cell.lblname.textColor=[UIColor whiteColor];
cell.lblsubtype.textColor=[UIColor whiteColor];
cell.lbltime.textColor=[UIColor whiteColor];
cell.lblname.font=[UIFont fontWithName:#"Merriweather" size:15.0];
cell.lblsubtype.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
cell.lbltime.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(1, cell.contentView.frame.size.height, 298.0,2)] ;
imageview.image = [UIImage imageNamed: #"lineseperator.png"];
[cell.contentView addSubview:imageview];
cell.lblname.text = lg.name;
NSLog(#"%#",lg.name);
[[cell imgtype] setImage:[UIImage imageNamed:lg.imagetype]];
if([lg.subtype length] == 0){
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.time;
cell.lbltime.text = #"";
}
else{
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.subtype;
cell.lbltime.text = lg.time;
}
return cell;
}
The imageview you're adding to the cell is covering all other content. You'll need to either resize/reposition it, or just use the already existing cell.imageView instead.
My table only has 2 sections. I have a UITextView as a subview in the 2nd section of my table. and a list of possible quotes in the first section.
I'm having a problem where once the user selects a particular quote which gets "pasted" into the UITextView like so:
replyTextView.text = [NSString stringWithFormat:#"#%# UserName writes... \n[\"%#\"]", replyPostCode,[separatedString objectAtIndex:indexPath.row]];
or types text into the textview, after they scroll away from the textview so it's off the screen it gets cleared. I guess this is because I keep releasing it from my table..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSString *replyCellIdentifier = #"replyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if ([indexPath section] == 0) {
cell = [self CreateMultilinesCell:CellIdentifier];
}
else if ([indexPath section] == 1) {
//NSLog(#"TextField");
cell = [self CreateMultilinesCell:replyCellIdentifier];
if ([indexPath row] == 0) {
replyTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
//replyTextView.adjustsFontSizeToFitWidth = YES;
replyTextView.textColor = [UIColor blackColor];
replyTextView.keyboardType = UIKeyboardTypeASCIICapable;
replyTextView.returnKeyType = UIReturnKeyDefault;
replyTextView.backgroundColor = [UIColor whiteColor];
replyTextView.autocorrectionType = UITextAutocorrectionTypeNo;
replyTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
replyTextView.textAlignment = UITextAlignmentLeft;
replyTextView.tag = 0;
replyTextView.editable = YES;
replyTextView.delegate = self;
replyTextView.scrollEnabled = YES;
//[replyTextView becomeFirstResponder];
//replyTextView.clearButtonMode = UITextFieldViewModeNever;
//[replyTextView setEnabled: YES];
[cell.contentView addSubview:replyTextView];
[replyTextView release];
//cell.detailTextLabel.text = #"";
}
}
}
//NSLog(#"%d", [indexPath section]);
if ([indexPath section] == 0) {
cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}
return cell;
}
I'm just wondering just what is the best way to keep the text in my UITextView when the user scrolls the uitextview off the screen and back again?
update
- (UITableViewCell*) CreateMultilinesCell :(NSString*)cellIdentifier
{
//NSLog(#"Entering CreateMultilinesCell");
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [self SubFont];
cell.detailTextLabel.textColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:33.0/255 alpha:1.0];
[cell setBackgroundColor:[UIColor clearColor]];//]colorWithRed:.98 green:.98 blue:.99 alpha:1.0]];
[self.tableView setBackgroundColor:[UIColor clearColor]];//colorWithRed:.94 green:.96 blue:.99 alpha:1.0]];
//NSLog(#"Exiting CreateMultilinesCell");
return cell;
}
The easiest solution is to use a different cell identifier for the two types of cells.
Edit: I see you are using two different types, but you are not taking that into account in the dequeue call.