I am trying to make quiz program. I used to UITableview for answers. How can i make to flashing on background when user click the correct answer? Is it possible?
Here is the some part of my project;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
//cell=[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [selectedCells containsObject:rowNsNum] )
{
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *text = [answers objectAtIndex:indexPath.row];
NSData *data = [text dataUsingEncoding: [NSString defaultCStringEncoding] ];
cell.textLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ];
//cell.textLabel.text=[answers objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
tableView.backgroundColor=[UIColor whiteColor];
cell.backgroundColor=[UIColor whiteColor];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvatica" size:15]];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
CGRect rect = cell.frame;
UIView * view = [[UIView alloc] init];
UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ListGray"] ];
BOOL selx;
if (answerType==2)
selx = [self isSelected:indexPath];
else
selx = [lastIndexPath isEqual:indexPath];
if (selx) {
back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"aaaa.png"] ];
cell.textLabel.textColor = [UIColor whiteColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
} else {
cell.accessoryView = nil;
if (lastIndexPath != nil){
cell.textLabel.textColor = [UIColor blackColor];
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
back = nil;
}
}
[view addSubview:back];
cell.backgroundView = view;
back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
tableView.allowsSelection = NO;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if (answerType==3) {
NSString *secilenAnswerID;
secilenAnswerID= [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
for (int a=0; a<[answers count]; a++) {
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:a inSection:0]];
cell. accessoryType= UITableViewCellAccessoryNone;
[selectedCells removeObject:rowNsNum];
selectedCells=[[NSMutableArray alloc]init];
}
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
isRadioSelected=TRUE;
radioAnswer = [genelAnswersID valueForKey:[answers objectAtIndex:indexPath.row]];
[selectedCells addObject:rowNsNum];
lastIndexPath = indexPath;
[tableView reloadData];
}
}
Sorry for my bad english. Thank you for your interest.
You can add a background view into your UITableViewCell with a backgroundColor. Then in your tableView: didSelectRowAtIndexPath: you can animate the hidden or alpha with the animation APIs in UIView. When the animation finishes you can animate hidden to TRUE or alpha back to 0 to get the flash effect.
Here is my question's solution. I took advice from Stephen Johnson:)
[UIView animateWithDuration:0.1f delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[UIView setAnimationRepeatCount:3];
cell.alpha = 0;
} completion: ^(BOOL finished) {
cell.hidden = YES;
[UIView animateWithDuration:0.1 animations:^{
cell.alpha = 1;
} completion: ^(BOOL finished) {
cell.hidden = NO;
}];
}];
Related
I'm creating a UISwitch programmatically in one of the tableview datasource function(cell for row at index). when I'm scrolling the table view, the OFF state switches are malfunctioned(UI) two rounds appeared. Attaching the screenshot of the switch.
Appreciate your help!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60, (cell.contentView.frame.size.height/2)-20 , 100, 100)];
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
[cell.contentView addSubview:myswitch];
myswitch.tag = indexPath.row;
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have faced the same issue and fixed with following code before creating switch
for(UIView *subView in cell. contentView.subviews){
if ([subView isKindOfClass:[UISwitch class]]) {
[subView removeFromSuperview];
}
}
Or
static NSString *TableIdentifier = #"YourCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
//place your all UI elements code here.
}
I have updated your code, try this will work for you,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *title = (UILabel*)[cell viewWithTag:80];
UILabel *description = (UILabel*)[cell viewWithTag:81];
UIView *innerView = (UIView *)[cell viewWithTag:82];
innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0] CGColor];
innerView.layer.borderWidth = 2.0f;
NSDictionary *displayDict = scenarioListsArray[indexPath.row];
title.text =[displayDict objectForKey:#"name"];
description.text = [displayDict objectForKey:#"description"];
UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
if(mySwitch == nil){
myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
[cell.contentView addSubview:myswitch];
[myswitch addTarget:self action:#selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
}
myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
myswitch.tag = indexPath.row+597; //597 is extra number added to tag
if ([[displayDict objectForKey:#"status"] isEqualToString:#"ACTIVE"]) {
[myswitch setOn:YES animated:YES];
}
else
{
[myswitch setOn:NO animated:YES];
}
return cell;
}
I have a problem with a table view.
When I selected row, my height of cell is changing and display some information. If I select a second time, it's closing.
Works like a charm on simulator, but on real device, first part work fine, but when I want close it, he is executing the code but probably not refreshing tableView, because my information stay displayed.
Another problem for real device: my views is not appear.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==1 &&show == YES) {
return 50 + descLabel;
}else if (indexPath.row == 2 && show2 == YES){
return caracLabel;
}
{
return 50;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellID = #"detailCell";
switch (indexPath.row) {
case 0:
cellID = #"detailCell1";
break;
case 1:
cellID = #"detailCell2";
break;
case 2:
cellID = #"detailCell3";
break;
case 3:
cellID = #"detailCell4";
default:
break;
}
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
if (indexPath.row == 1) {
if (show == NO) {
cell.labelDef.hidden =YES;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==1) {
if (show == NO ) {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell2" forIndexPath:indexPath];
show=YES;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.labelDef.text=[self.desc objectAtIndex:pos];
cell.labelDef.sizeToFit;
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_5#3x.png"];
cell.labelDef.frame = CGRectMake(10, 50, cell.labelDef.frame.size.width, cell.labelDef.frame.size.height);
cell.labelDef.hidden = NO;
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 50, cell.frame.size.width, cell.labelDef.frame.size.height)];
[view setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:view];
[cell.contentView addSubview:cell.labelDef];
descLabel = cell.labelDef.frame.size.height ;
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}else{
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell2" forIndexPath:indexPath];
show= NO;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_2#3x.png"];
cell.labelDef.hidden = YES;
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}else if (indexPath.row == 2) {
if ( show2 == NO) {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell3"forIndexPath:indexPath];
show2=YES;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_5#3x.png"];
NSArray *firstWords = [[self.carac objectAtIndex:pos] componentsSeparatedByString:#"\n"];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 50, cell.frame.size.width , 100)];
[view1 setBackgroundColor:[UIColor colorWithRed:246/256.0 green:245/256.0 blue:241/256.0 alpha:1]];
[cell.contentView addSubview:view1];
caracLabel = 50;
for (int i = 0; i <firstWords.count; i++) {
NSArray *array = [[firstWords objectAtIndex:i]componentsSeparatedByString:#"/"];
UILabel *label = [[UILabel alloc]initWithFrame: CGRectMake(10, caracLabel, 90, 20)];
label.text = [array objectAtIndex:0];
label.textColor = [UIColor colorWithRed:150/256.0 green:150/256.0 blue:150/256.0 alpha:1];
label.numberOfLines = 0;
[label sizeToFit];
[cell.contentView addSubview:label];
UILabel *label1 = [[UILabel alloc]initWithFrame: CGRectMake(110, caracLabel, 205, 20)];
label1.text = [array objectAtIndex:1];
label1.textColor = [UIColor colorWithRed:150/256.0 green:150/256.0 blue:150/256.0 alpha:1];
label1.numberOfLines = 0;
[label1 sizeToFit];
caracLabel = caracLabel + label1.frame.size.height;
if (i != firstWords.count - 1) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, caracLabel, cell.frame.size.width, 1)];
[view setBackgroundColor:[UIColor whiteColor]];
caracLabel =caracLabel+2;
[cell.contentView addSubview:view];
}
[cell.contentView addSubview:label1];
}
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 1, caracLabel - 50)];
[view setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:view];
view1.frame = CGRectMake(0, 50, cell.frame.size.width, caracLabel-50);
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
detailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"detailCell3"forIndexPath:indexPath];
show2= NO;
cell.label.text = [self.array objectAtIndex:indexPath.row];
cell.image1.image = [UIImage imageNamed:[NSString stringWithFormat:[self.imageArray objectAtIndex:indexPath.row]]];
cell.imageButton.image = [UIImage imageNamed:#"Play_Symbol_copie_2#3x.png"];
// [self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}else
if (indexPath.row == 3){
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.wiki objectAtIndex:pos]]];
UIWebView *video = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - +50)];
[video loadRequest:request];
video.delegate=self;
showWeb = YES;
UIActivityIndicatorView *actInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
actInd.color=[UIColor blackColor];
[actInd setCenter:self.view.center];
self.activityIndicator=actInd;
[self.view addSubview:video];
[video addSubview:self.activityIndicator];
}
}
This is how looking/work on simulator :
closed-
description-
caracteristics-
And this is how works for real device:
description-
caracteristics-
closed-
and
I don't know if that is problem, but row is changing colour after i press first time in real device. Why? Any ideas?
If everything working well in simulator but not in Device then check "dequeueReusableCellWithIdentifier"/"CellID" values because simulator is not case sensitive, but device is case sensitive.
It might be not find out your cell ID so it happen, I'm not sure about it but just check once.
I am trying to create a collapse table like this
My code :
- (WordListTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"wordListTableCell";
WordListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[WordListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *uu = [wordList objectAtIndex:indexPath.row];
if(!addingNewCellFlag)
{
if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"en"])
{
cell.textLabel.text = [uu valueForKey:#"tiitle_en"];
}
else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"ru"])
{
cell.textLabel.text = [uu valueForKey:#"tiitle_ru"];
}
UIImage *buttonImage;
if(indexPath.row==0)
{
buttonImage = [[UIImage imageNamed:#"rowFirst.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
}
else
{
buttonImage = [UIImage imageNamed:#"rowMiddle.png"];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.isCanExpanded = TRUE;
}
else
{
if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"en"]){
cell.textLabel.text = [uu valueForKey:#"content_en"];
}
else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:#"ru"])
{
cell.textLabel.text = [uu valueForKey:#"content_ru"];
}
cell.backgroundColor = [UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.textAlignment = NSTextAlignmentJustified;
cell.isCanExpanded = FALSE;
}
[cell setIsExpanded:FALSE];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WordListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
addingNewCellFlag = TRUE;
if(cell.isCanExpanded)
{
if(![cell isExpanded]){
[cell setIsExpanded:TRUE];
NSDictionary *uu = [wordList objectAtIndex:indexPath.row];
[wordList insertObject:uu atIndex:indexPath.row+1];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]
withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
else if ([cell isExpanded])
{
[cell setIsExpanded:FALSE];
[wordList removeObjectAtIndex:indexPath.row+1];
[self.tableView beginUpdates];
NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
}
cell = nil;
}
It works, if we click on cell we create disruption cell, is the new cell has right text ([uu valueForKey:#"content_en"]), but have wrong backgroundColor (cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];). But sometimes the backgroundColor is right ([UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1]).
Please, tell me, how does this method works.
I don't see your code in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Maybe it's wrong when you deal with the cells of different indexpaths.
I develop application contains UITableView (not Controller) and it was added programmatically. I didn't use any nib file. and found out that in simulator work fine but's when I build on device Table shown up but didn't see any text on it.
Datasource and delegate is already connected.
here some code
if (kPortrait) {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(568, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
} else {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(824, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
}
[optionTV setAutoresizesSubviews:YES];
[optionTV setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[optionTV setDataSource:self];
[optionTV setDelegate:self];
optionTV.alpha = 0.0f;
optionTV.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.50];
[self.view addSubview:optionTV];
[UIView animateWithDuration:0.5f animations:^{
trafficBT.transform = CGAffineTransformMakeTranslation(-200, 0);
optionTV.alpha = 0.8;
}];
for delegate method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"tableView Data num row: %d", [optionArray count]);
return [optionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
if (kIsIPhone) {
cell.textLabel.font = [UIFont systemFontOfSize:18];
} else {
cell.textLabel.font = [UIFont systemFontOfSize:20];
}
cell.textLabel.text = [optionArray objectAtIndex:indexPath.row];
return cell;
}
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.