UITableView change cells - ios

I have a UITableView with this code below:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:view];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != 6) {
return 65;
}else{
return 204;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 7;
}
This code is very simple, the problem with him is:
When I run my app first time, I can see my fields organized in the manner prescribed (Like I put in cellforRowAtIndexPath)
When I scroll down (put my fields out of window) and scroll to up again I see a problem!
The problem is that my table, create a UITextview in rows like 0, 1 and 2. But why this is happening? in my code I made it clear! That only the row 6 will be created a UITextView!
How can I solve this problem?

Two issues:
1) your cell is recycled, this means when the cell gets to row 6 your UITextView is added to the cell and then when scrolling back the UITextView is still there
2) and you should add the subviews of cell in its contentView and not the cell itself.
Solution :
Use tow kind of UITableViewCell, one specific for row 6 and another one for the other rows. Register the cells and then dequeue them for the appropriate indexPath.

I think your problem is loading your NIB file into the table view.
Look into Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
I tried to recreate your problem by creating a UITableViewCell using Interface Builder called ItemCell and it works. Make sure you have this in your viewDidLoad
[super viewDidLoad];
//Load the NIB file
UINib *nib = [UINib nibWithNibName:#"ItemCell"
bundle:nil];
//Register this NIB, which contains the cell
[self.tableView registerNib:nib
forCellReuseIdentifier:#"ItemCell"];
Then replace
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
with
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"ItemCell"];
}
You might want to also look into putting your images in a data structure to go with a MVC design pattern.

Give table Row 6 a different identifier than the others
static NSString *CellIdentifier = #"MyCell";
if (indexPath.row == 6){
CellIdentifier = #"MyCell6";
}
Even then you shouldn't be re allocing the textField everytime cellForRowAtIndexPath is called. That's the point of reusing the cell! But that's another conversation so look up how to properly recycle cells

Put this code in viewDidLoad and make you view a global variable.
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
And now create the tableView like this:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
view.hidden = YES;
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
view.hidden = NO;
[cell addSubview:view];
}
return cell;
}

Related

Second TableView is not showing on the screen

I have two tableviews in a menu controller. first tableview populates a dynamic menu list from db and second tableview should only display the strings I tell it. So right now I only need 2 cells, Settings and Login. The first table view works fine. But, the second is not displaying the items. code bellow represent the second tableview
ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
}
Main table
-(void)setMenuItems:(NSArray *)menuItems
{
if(_menuItems != menuItems)
{
_menuItems = menuItems;
}
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
Second table
-(void)setExtraMenuItems:(NSArray *)extraMenuItems
{
if(_extraMenuItems != extraMenuItems)
{
_extraMenuItems = extraMenuItems;
}
[self.extraTableView reloadData];
}
- (NSInteger)extraTableView:(UITableView *)extraTableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.extraMenuItems.count;
}
- (UITableViewCell *)extraTableView:(UITableView *)extraTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
What is wrong with it?
You shouldn't rename the tableView delegate and datasource methods: just test the tableView parameter that is passed to them, to determine which tableView they relate to. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if (tableView == self.extraTableView) {
return self.extraMenuItems.count;
} else {
return self.menuItems.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.extraTableView) {
NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
} else {
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
}
And likewise for all the other tableView delegate and datasource methods. You also need to make sure that the delegate and datasource are set for both table views. You can do either do this in your storyboard, or in code eg. in viewDidLoad:
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
EDIT
You don't need both extraMenuItems and extraMenuItemFiller. I would use just extraMenuItems. Load it with the two values in viewDidLoad as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraMenuItems = #[#"Login",#"Settings"];
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
}
and amend the cellForRowAtIndexPath to use extraMenuItems rather than extraMenuItemFiller:
NSString *cellValue = [self.extraMenuItems objectAtIndex:indexPath.row];

custom cell data not displayed ios

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.

uitableviewcell background color becomes clearcolor

I have problem with the cell background color becoming clearcolor always. I set the uiview background color to gray color, tableview background color to clear color and I did not set tableviewcell background color to clear color. But the cell background always appears grey. Can any one have any idea about this.
Thanks
-(void)viewDidLoad
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"TableBackGround.png"]];
Acc_Details_TView.backgroundColor = [UIColor clearColor];
Acc_Details_TView.rowHeight = 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
case 0:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"TableCell"] autorelease];
cell.backgroundColor =[UIColor clearColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Date"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
}
NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Title"] ;
if ([titleName length] > 19) {
cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];
}
else{
cell.textLabel.text = titleName;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
acc_Amount.textAlignment = UITextAlignmentRight;
acc_Amount.backgroundColor = [UIColor clearColor];
acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Amount"];
acc_Amount.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:acc_Amount];
UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
balance_Amount.textAlignment = UITextAlignmentRight;
balance_Amount.text = #"$1234.50";
balance_Amount.backgroundColor = [UIColor clearColor];
balance_Amount.textColor = [UIColor grayColor];
balance_Amount.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:balance_Amount];
return cell;
}
}
}
Try setting your cell's background colour in the method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
rather than in your cellForRowAtIndexPath: method.
Your cells are not exactly transparent. Setting UITableView's backgroundColor does some crazy undocumented stuff. Best way to see this is to set it to some semi-transparent color like [UIColor colorWithWhite:0.5 alpha:0.5] by which you get something like this:
To fix your problem, you will have to set cells' contentView.backgroundColor and backgroundColors of all the subviews after setting tableView's. Here is your cellForRowAtIndexPath: updated with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIColor *cellBackgroundColor = [UIColor whiteColor];
switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
case 0:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"TableCell"] autorelease];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Date"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
cell.accessoryView.backgroundColor = cellBackgroundColor;
cell.backgroundView.backgroundColor = cellBackgroundColor;
cell.contentView.backgroundColor = cellBackgroundColor;
cell.textLabel.backgroundColor = cellBackgroundColor;
cell.detailTextLabel.backgroundColor = cellBackgroundColor;
UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
backView.backgroundColor = cellBackgroundColor;
cell.backgroundView = backView;
[backView release];
}
NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Title"] ;
if ([titleName length] > 19) {
cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];
}
else{
cell.textLabel.text = titleName;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
acc_Amount.textAlignment = UITextAlignmentRight;
acc_Amount.backgroundColor = cellBackgroundColor;
acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Amount"];
acc_Amount.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:acc_Amount];
UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
balance_Amount.textAlignment = UITextAlignmentRight;
balance_Amount.text = #"$1234.50";
balance_Amount.backgroundColor = cellBackgroundColor];
balance_Amount.textColor = [UIColor grayColor];
balance_Amount.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:balance_Amount];
}
}
return cell;
}
I didn't understand your question. You set the table's background to grey, then clear, but you didn't set it as clear, and then it appears grey, which you didn't want even though you set it as grey?
table.backgroundColor = [UIColor clearColor];

Keeping text in UITextView after scrolling it off the screen

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.

UItableView multiplle data printed

I am creating grouped table in my application.
It works fine. But when I scroll the table upward and backward reprinted text on previous cell. It look like horrible.
This is my Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
/*
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
*/
if(indexPath.section == 0)
{
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
float rowHeight = [DetailViewController calculateHeightOfTextFromWidth:[dict objectForKey:#"golf_name"] :[UIFont boldSystemFontOfSize:18] :290 :UILineBreakModeTailTruncation];
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, rowHeight)];
categorName.font = [UIFont boldSystemFontOfSize:20];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor whiteColor];
categorName.text = [dict objectForKey:#"golf_name"];
[cell addSubview:categorName];
[categorName release];
}
if(indexPath.section == 1)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 70, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Phone";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 290, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_phone"];
[cell addSubview:phone];
[phone release];
}
if(indexPath.section == 2)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 90, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Home page";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 230, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_url"];
[cell addSubview:phone];
[phone release];
}
.......
.....
return cell;
}
Update
My original answer was not the correct way to reuse UITableViewCell's. You should never use "CellID%d%d"(indexPath.row & indexPath.column) as cell identifier, that defeats the very purpose of cell reuse. It will create separate UITableViewCell for every row in the table, and every one of those cells stays in memory. Imagine what will happen if you have a tableView with around 1000 rows.
The real answer to OP's question is - make sure to reset a UITableViewCell returned by dequeueReusableCellWithIdentifier because it might return a cell that is already used and its UI populated. For example, if you didn't reset the texts in UILabel, it might contain strings that should have been shown in a different row of your tableView.
My original Answer (Don't use this)
Hiren, just try,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
}
instead of
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
Please read this..you will understand how to give cell identifiers..
When you create the UITableView object, Set the rowHeight for each cell.
myTableView.rowHeight = 50; //Depend on your requirement,
Let me know if you still struggle to get the desire result.
If your cell's height is not constant then implement heightForRowAtIndexPath method,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And return different-2 height for each cell.
try adding any color because clearcolor is making label transparent.

Resources