How do I get the default CGRECT dimensions of uitableviewcell's detailtextlabel? - objective-c

We're hoping to add a UIextLabel that replaces the default detailTextLabel of UITableViewCell's UITableViewCellStyleSubtitle. I can get the UITextLabel to show however the problem is that I can't get it aligned as if it were a detailTextLabel. The following code I've tried is this:
switch (indexPath.row) {
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
//cell.detailTextLabel.text = #"Enter your display name";
//self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.detailTextLabel.frame.origin.x, cell.detailTextLabel.frame.origin.y, cell.detailTextLabel.frame.size.height, cell.detailTextLabel.frame.size.width)];
self.displayNameTextField.placeholder = #"Enter your display name";
self.displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[self.displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:self.displayNameTextField];
break;
}
default:
break;
}

A small correction in your code. You can use your own code withe some changes.
Try this. It will work.
switch (indexPath.row){
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
UITextField *displayNameTextField = [[UITextField alloc] initWithFrame:cell.detailTextLabel.frame];
displayNameTextField.placeholder = #"Enter your display name";
displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:displayNameTextField];
break;
}
default:
break;
}

Related

Disable UITextField in UITableViewCell

I have a UITextField in a UITableViewCell.
Even though I set -
textField.userInteractionEnabled = NO;
textField.enabled = NO
But when I click on the table cell which contains the textField, the keyboard comes up for the textfield.
Why is this happening and how can I prevent it?
EDIT: Strangely this is happening when I first set some text in the textfield. When the textfield is empty, it is not editable.
EDIT: Code for cellForRowAtIndexPath -
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;
if (_editingNotAllowed) {
[textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
[textField setUserInteractionEnabled:NO];
textField.enabled = NO;
} else {
[textField setUserInteractionEnabled:YES];
}
[cell.contentView addSubview:textField];
You should create an UITableViewCell as shown in this repo :)
https://github.com/breeno/EditingUITableView
And use your custom UITableViewCell like this:
if(condition){ // Check the row must be TextField
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(!cell){
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.label.text = #"Title Row"; //UITextLabel Title
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
cell.textField.returnKeyType = UIReturnKeyDone;
cell.textField.backgroundColor = [UIColor clearColor];
cell.textField.textAlignment = NSTextAlignmentLeft;
cell.textField.tag = 1;
} else { // Normal UITableViewCell
}

UITableView section not reloading when called

I want a section of my table view to reload whenever the ViewWillAppear method is called, I've implemented this like so:
- (void)viewWillAppear:(BOOL)animated {
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:0 inSection:1];
reloadRows = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
}
Here is the rowforsection method that indicates which content should appear in each tableview section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"fadk");
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"PINGAS"];
[self.tableView setAlwaysBounceVertical:YES];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"PINGAS"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
// if ([indexPath section] == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 300, 41)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 120)];
UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)] autorelease];
paddingView.backgroundColor = [UIColor cyanColor];
// if ([indexPath row] == 0) {
if ([indexPath section] == 0) {
NSLog(#"0");
[cell addSubview:textField];
if ([indexPath row] == 0) {
textField.placeholder = #"Title";
}
else{
textField.placeholder = #"Location";
}
}
else if ([indexPath section] == 1) {
NSLog(#"1");
NSDateFormatter *formatter;
NSString *eSString1;
NSString *eEString2;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
cell.textLabel.text = #"Starts\nEnds";
cell.textLabel.numberOfLines = 2;
eSString1 = [formatter stringFromDate:eSTime];
eEString2 = [formatter stringFromDate:eEtime];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%#", eSString1, eEString2];
cell.detailTextLabel.numberOfLines = 2;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
NSLog(#"2");
[cell addSubview:textView];
}
textField.delegate = self;
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor blackColor];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textField.textAlignment = UITextAlignmentLeft;
textField.tag = 0;
//playerTextField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[textField setEnabled: YES];
[textField release];
textView.delegate = self;
textView.textColor = [UIColor blackColor];
textView.keyboardType = UIKeyboardTypeAlphabet;
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor = [UIColor clearColor];
textView.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textView.textAlignment = UITextAlignmentLeft;
textView.tag = 0;
[textView release];
// }
}
return cell;
}
This works swimmingly the first load, and I after the first calling of viewWillAppear, but after that the section seems to recycle the data from the first load and the second load, and while it still enters the cellforrow section, it no longer goes into the section I call in the viewWIllAppear section.
The reload should be sandwiched between begin / end updates:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

EXC_BAD_ACCESS when I scroll my view

I have two views: the first one is a UITableView in plain style (created programmatically); the second one is a UIScrollView that contains an image and a UITableView in grouped style (also created programmatically).
When I navigate from the firstView -> secondView everything works fine. However if I come back to the firstView and then I try to navigate firstView ->secondView for the second time, I get a EXC_BAD_ACCESS error.
UPDATE
This is the code of the UITableView delegate in the secondView:
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
//header - breve descrizione
return 2;
break;
case 1: //mappa
//header - mappa statica (immagine) - footer (indirizzo)
return 3;
break;
case 2: //review
//header - prima review
return 2;
break;
default:
return 0;
break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_DESCR_HEADER;
break;
case 1: //case corpo
return HTABLE_DESCR_BODY;
break;
default:
return 0;
break;
}
break;
case 1:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_LOC_HEADER;
break;
case 1: //case corpo
return HTABLE_LOC_BODY;
break;
case 2: //case footer
return HTABLE_LOC_FOOTER;
break;
default:
return 0;
break;
}
break;
case 2:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_REV_HEADER;
break;
case 1: //case corpo
return HTABLE_REV_BODY;
break;
default:
return 0;
break;
}
break;
default:
return 0;
break;
}
}
/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Travellers Guide";
}*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int section = 2;
//se ci sono review
if ([[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count] > 0) {
section++;
}
return section;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self getCellContentView:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
switch (indexPath.section) {
case 0: //descrizione
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = #"Descrizione";
} else {
cell.textLabel.text = #"Description";
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaDescrHeader.png"]];
break;
case 1: //descrizione
NSLog(#"Descr");
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaRevBody.png"]];
cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"description"];
cell.textLabel.font = [UIFont systemFontOfSize:10.0];
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 3;
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset =CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
break;
default:
break;
}
break;
case 1: //mappa
//header - mappa statica (immagine) - footer (indirizzo)
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = #"Posizione";
} else {
cell.textLabel.text = #"Location";
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocHeader.png"]];
break;
case 1: //mappa
NSLog(#"Mappa");
CLLocationDegrees latitude = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longitude = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"longitude"] doubleValue];
CLLocation* poiLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
Annotation *ann = [Annotation annotationWithCoordinate:poiLocation.coordinate];
//mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, HTABLE_LOC_BODY)];
[mapView setHidden:NO];
[mapView addAnnotation:ann];
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = poiLocation.coordinate;
region.span.longitudeDelta = 0.05f;
region.span.latitudeDelta = 0.05f;
[self.mapView setRegion:region animated:YES];
[self.mapView regionThatFits:region];
cell.backgroundView = mapView;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 302, 120)];
imgView.image = [UIImage imageNamed:#"ombraMappa.png"];
[cell.backgroundView addSubview:imgView];
[imgView release];
break;
case 2: //footer
cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"locality"];
cell.textLabel.font =[UIFont systemFontOfSize:10.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.detailTextLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"address"];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:9.0];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.shadowColor = [UIColor whiteColor];
cell.detailTextLabel.shadowOffset = CGSizeMake(1, 1);
cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocFooter.png"]];
break;
default:
break;
}
break;
case 2: //review
//header - mappa statica (immagine) - footer (indirizzo)
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = [NSString stringWithFormat:#"Recensioni (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count]];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"Review (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count]];
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocHeader.png"]];
break;
case 1: //review
cell.textLabel.text = [[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] objectAtIndex:0] objectForKey:#"review"];
cell.textLabel.font =[UIFont systemFontOfSize:10.0];
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 3;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaRevBody.png"]];
break;
default:
break;
}
break;
default:
break;
}
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
return cell;
}
The EXC_BAD_ACCESS isn't caused by the progression of one view to the next. Rather, it will be properties of those views, or the creation and release - or not - of the view objects themselves that are to blame.
Your first step is to run the Analyzer. Then, if you've fixed all that and theres still a problem, start running the Leaks tool in Instruments.
For more details on resolving EXC_BAD_ACCESS issues, along with the excellent link on what causes these errors and step by step instructions for how to fix, have a look at answers to these questions:
finding reason for EXC_BAD_ACCESS - in Xcode4
Random EXC_BAD_ACCESS in a place that it cannot happen
In your case, I'd specifically look at how you are creating secondView, and what you do with firstView when you return to secondView.
I just spent a week tracking down a EXC_BAD_ACCESS problem in a large app that worked fine before converting it to ARC. I would get the EXC_BAD_ACCESS on the closing bracket of a "switch" construct. Yet NSZombieEnabled and the various Instrument settings would not detect anything for me.
What fixed it was putting curlies around the case's that made the difference. I did it for each of them, though perhaps it was critical for only some, i.e.
Before:
case ...:
stmt;
stmt;
break;
After:
case ...:{
stmt;
stmt;
}break;
This fixed the problem in two places within a large app for me. I know that I read something somewhere about ARC and switch/case (and maybe someone can add a link to it), but I don't understand the theory behind why this happened and why this fix worked. Perhaps someone could explain. (Later: after reading the following reference at openradar, it all makes sense --- there is something wrong in the "cleanup code" generated between case statements --- a bogus "release" is generated and that soon causes the EXC_BAD_ACCESS.)
I also discovered this, which might be related: http://openradar.appspot.com/11329693

how can i set the table view cell property in an iphone app

I have a table view controller like this, and when a row is clicked its colour will turn to blue, but I don't want it to, because I have a text field and a label in each row. How can I stop it from becoming blue like this?
This is my code for cell construction:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].text =#"";
tfText[0].placeholder =#"<Student Name>";
cell.primaryLabel.text =#"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].placeholder =#"<Student Age>";
cell.primaryLabel.text =#"Student Age: ";
}
break;
case 2:
{
cell.primaryLabel.text =#"Gender: ";
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.selectedSegmentIndex = 1;
segmentedControl.tintColor = [UIColor purpleColor];
segmentedControl.backgroundColor = [UIColor whiteColor];
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView =segmentedControl;
//[self.view addSubview:segmentedControl];
/*
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
*/
}
break;
case 3:
{
tfText[3] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[3];
tfText[3].delegate=self;
tfText[3].placeholder =#"<DD/MM/YYYY>";
cell.primaryLabel.text =#"Date Of Birth: ";
}
break;
case 4:
{
tfText[5] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[5];
tfText[5].delegate=self;
tfText[5].placeholder =#"<Blood Group>";
cell.primaryLabel.text =#"Blood Group: ";
}
break;
case 5:
{
tfText[6] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[6];
tfText[6].delegate=self;
tfText[6].placeholder =#"<Address>";
cell.primaryLabel.text =#"Address: ";
}
break;
default:
break;
}
Actually it is only one line of code...
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Inside your method i.e cellforrowatindexpath add the below code
UIView *bckv = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width,cell.frame.size.height)];
bckv.backgroundColor = [UIColor colorWithRed:106/256.0 green:108.0/256.0 blue:107.0/256.0 alpha:0.0];
cell.selectedBackgroundView=bckv;
[bckv release];
OR
cell.selectionStyle=UITableViewCellSelectionStyleNone;
Hope it helps......
cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITextField in UITableView cell is returning null

I've been banging my head against the wall on this one for quite some time now. Any input or direction is greatly appreciated.
So the goal is the create a log in form from text fields in a table. That user info, once collected will be passed on to an array in a seperate view controller so in can stored in a "favourites" list.
So I've created the form which looks great and all but when I console out the form results the fields are return (null). I've picked the site for answers but can't anything exact. Below is the cellForRowAtIndexPath: method I feel that maybe the issue is where I'm creating the textFields. Again, any input is appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
[textField retain];
}
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:#"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 0;
break;
case 1:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 1;
break;
case 2:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 2;
break;
case 3:
textField.placeholder = #"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 3;
break;
case 4:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 4;
break;
case 5:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.tag = 5;
break;
}
}
[textField setEnabled: YES];
[cell addSubview:textField];
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
return cell;
}
The textField variable (I assume it is an ivar in the class or a static global variable) is your main problem. You create a new text field each time you create a new cell, which is fine, but then you add it to a cell every time the cellForRowAtIndexPath method is called. Since cells are reused this will screw things up.
Your code need to look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
textfield.tag = 1;
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:#"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
[textField setEnabled: YES];
[cell.contentView addSubview:textField];
[textField release];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:1];
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 1:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 2:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 3:
textField.placeholder = #"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 4:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 5:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
break;
}
}
textField.text = [listData objectAtIndex:indexPath.row];
return cell;
}
Not sure this does exactly what you want but it should point you in the right direction.
Create a custom cell to place your text field, for the love of god. You shouldn't have addSubview: related code in your tableView:cellForRowAtIndexPath:; just code that allocates the cell, and configures the cell enough so that the cell itself can display things they way you want them.
Look at the table view suite for an example of how to use custom cells. I believe 4_ and 5_ have custom cells.