cell.detailTextLabel not working - xcode

I want to set up a textLabel and a detailTextLabel for my table. The textLabel is working properly. However, I couldnt get the detailTextLabel to display the text that I have set. Below is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"SomeString";
cell.detailTextLabel.text = #"hello";
return cell;
}

I have faced similar issue as like #user1829700.
My initial settings are
TableView --> Prototype Cell --> (Attribute Inspector) Style - set to
Custom (by design)
Changed Style to - Subtitle, it does the trick

Try this
Use appropriate UITableViewCellStyle with this (anything but UITableViewCellStyleDefault should thus work). The cell's style is specified when you initialize it.

In your custom table cell implementation try adding #synthesize detailTextLabel;
Header (.h):
#property (nonatomic, weak) IBOutlet UILabel *textLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
Implementation (.m):
#synthesize textLabel;
#synthesize detailTextLabel;

Related

UITableViewController shows the same cell after updating to xcode 6

My UITableViewController works not properly after updating xcode to version 6.
Im using custom cells in my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BookDataTableViewCell * cell = (BookDataTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"BookDataTableViewCell"];
BookData * data = [_searchResult objectAtIndex:indexPath.row];
[cell setBookData:data];
NSLog(#"BookDataTableViewController: cell pointer = %p", cell);
return cell;
}
// BookDataTableViewCell.m
#interface BookDataTableViewCell()
#property (weak, nonatomic) IBOutlet UILabel * bookTitle;
#property (weak, nonatomic) IBOutlet UILabel * bookAuthor;
#end
#implementation BookDataTableViewCell
-(void)setBookData:(BookData *)bookData{
_bookData = bookData;
NSLog(#"BookDataTableViewCell: _bookTitle pointer = %p", _bookTitle);
NSLog(#"BookDataTableViewCell: bookTitle = %#", _bookData.title); // allways different!!!
[[self bookTitle] setText:_bookData.title];
[[self bookAuthor] setText:_bookData.author];
}
}
After updating xcode to version 6 all controller cells show the same book info using the last BookData in _searchResult list, although the data passed to BookDataTableViewCell are allways different.
Yesterday, befor update, the controller worked as expected...
Some idea what might have gone wrong?

xCode 4.5 Navigation Controller and back button how to save values

In my code I created this
#import <UIKit/UIKit.h>
#interface ViewControllerTable : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (strong, nonatomic) NSString *plcVar;
#end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
listaPlacas *placas = [[listaPlacas alloc]init];
[self.navigationController pushViewController:placas animated:YES];
}
But in the other view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *placaVar = cell.textLabel.text;
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
VCT.plcVar = [[NSString alloc]init];
VCT.plcVar = placaVar;
}
When I click in back button the value in variable plcVar is empty.
I assume that "other view" is the viewController you push to from your first ViewController, and you are trying to set a property in the firstViewController from your otherViewController.
Then here is where you go wrong:
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
You are creating a new instance of VCT, when in fact you want a reference to the VCT that you pushed from.
You can get this via your navigation controller...
NSArray* viewControllers = [self.navigationController viewControllers];
ViewControllerTable* VCT = [viewControllers objectAtIndex:
[ viewControllers count]-2];

Collection View Cell Won't Allow Me to Select

This is probably a really easy fix; I have been reading countless forums and tutorials and can't seem to find answer.
In my app I have a popover collection view to select a specific option and return a value...
However it won't highlight or select? I can see the NS Log Outputs never show a selection or datapass.
Here is my code...
collection.h file
#interface collection : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
#property (nonatomic, retain) NSArray *counterImages;
#property (nonatomic, retain) NSArray *descriptions;
#property (nonatomic, weak) UILabel *graniteselect;
#property (nonatomic, retain) UIPopoverController* _collectionPopOver;
#property (nonatomic) BOOL clearsSelectionOnViewWillAppear; // defaults to YES, and if YES, any selection is cleared in viewWillAppear:
#property (nonatomic) BOOL allowsSelection;
#end
implementation file:
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return _descriptions.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:_counterImages[row]];
myCell.imageView.image = image;
myCell.celltext.text= [_descriptions objectAtIndex:row];
return myCell;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
_graniteselect.text = [_descriptions objectAtIndex:row];
[self._collectionPopOver dismissPopoverAnimated:YES];
NSLog(#"Setting Value for Granite Select");
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath: (NSIndexPath *)indexPath {
// TODO: Deselect item
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
RoleDetailTVC *destination =
[segue destinationViewController];
destination.graniteselect = _graniteselect;
NSLog(#"Passing Data to RoleDetailTVC");
}
#end
There are a number of issues here. Firstly, I think you mixed up the delegate methods for selection. The collectionView:performAction:forItemAtIndexPath:withSender: method is only for special menu popovers and has nothing to do with selecting a cell per se. What you need to implement in your delegate is this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected cell at index path %#", indexPath);
}
right now you're just implementing the deselect callback.
Beyond that, also make sure that your image view in the UICollectionViewCell has touches enabled, otherwise you might be blocking touch events to reach the cell and it won't fire any delegate calls. I hope that helps!

Modify uilabel from a view to other - iphone sdk

I have TableViewController & DetailViewController.
I'd like to modify one label named 'label' in DetailViewController.
TableViewController.h :
#interface CodexTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *listOfItems;
DetailViewController *vc;
}
#property (nonatomic, retain) DetailViewController *vc;
#property (strong, nonatomic) IBOutlet UITableView *myTable;
#end
And an extract of my TableViewController.m :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vc.label.text = #"bonjour";
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(#"%#",cell.textLabel.text);
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
But it doesn't works.
Someone know the error ?
Please help me.
Thanks :)
You mix up two variables for your detail view controller. Remove the dvController one and only work with your vc property, like so:
vc = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[[vc label] setText:#"bonjour"];
Make label a property of your detail view controller and have it point at your onscreen label.

Xcode: iPad keyboard troubles (not that simple)

I am making an app which uses many Textfields. Most of them are inside static tableViews. I use the split view application template. Every category selected from the left panel presents a storyboard scene inside a second view on the right panel.
I just want to get rid of the keyboard with the "done" button however everything i have tried that would work on a simple view fails to work under these circumstances.
Can you please help me out with this?
p.s. I try to dismiss the keyboard inside the implementation file of the presented storyboard scene. Should i do something inside the Detail Scene of the split view controller?
Here is my Scene's code:
.h
#import <UIKit/UIKit.h>
#interface AfoEsoda : UITableViewController <UITextFieldDelegate>{
}
#property (strong, nonatomic) IBOutlet UITextField *merismataTF;
-(IBAction)hideKeyboard:(id)sender;
#end
.m
#synthesize merismataTF;
- (void)viewDidLoad
{
[super viewDidLoad];
merismataTF.delegate=self ;
}
//---------Hide Keyboard-------------------
//Tried but didn't work
-(IBAction)hideKeyboard:(id)sender {
[merismataTF resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
//Of course i do not use both methods at the same time.
EDIT:
When i set the textfield's delegate to self i get this crash:
Try implementing the textField's delegate, set the delegate to self, and in the delegate's method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
set
[textField resignFirstResponder];
Another way could be going through all of the view's subviews and if it is a text field, resign first responder:
for(int i=0;i<self.view.subviews.count;i++)
{
if([[self.view.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
{
if([[self.view.subviews objectAtIndex:i] isFirstResponder])
[[self.view.subviews objectAtIndex:i] resignFirstResponder];
}}
OK, I got it. Use this with with the textFieldShouldReturn method.
So here is your answer: You have declared your text field as a property and then use alloc and init it over and over again for each cell. Probably it only works properly for the last row.
Here is an example of how your cellForRow method should look like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *cellIdentifier = #"My cell identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *newTextField;
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
newTextField = [[UITextField alloc] initWithFrame:CGRectMake:(0,0,25,25)];
newTextField.tag = 1;
newTextField.delegate = self;
[cell.contentView addSubview:newTextField];
}
else
newTextField = (UITextField *)[cell.contentView viewWithTag:1];
And then, if you need the textField's value for a certaing row, simply use:
UITextField *someTextField = (UITextField *)[[tableView cellForRowAtIndexPath:indexPath].contentView viewWithTag:1];
NSLog(#"textField.text = %#", someTextField.text);

Resources