I am using a Storyboard's prototype cell with a custom table cell class and the UILabels are nil in cellForRowAtIndexPath. Xcode's Identifier is correct, the cell is initialized, and the default UILabels (i.e. textLabel and detailTextLabel) are initialized but not the custom UILabels I added and setup IBOutlets for. A couple things I have tried:
I tried removing the registerClass calls in ViewDidLoad but that crashes since the cells need it to be initialized as the custom class GenericDetailCell.
viewWithTag returns nil
I tried iterating through all the subviews of UITableView to see if I was getting the wrong cell. The correct cell is getting returned by dequeueReusableCellWithIdentifier
Has anyone run into this?
#implementation PeopleGroupPickerViewController
{
NSArray *people;
NSArray *searchResults;
}
- (void)viewDidLoad
{
[super viewDidLoad];
people = [[DAL sharedInstance] getPeople:false];
[self.tableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:#"PersonCell"];
[self.searchDisplayController.searchResultsTableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:#"PersonCell"];
// stackoverflow.com/questions/5474529
[self.searchDisplayController setActive:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return searchResults.count;
}
else
{
return people.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PersonCell" forIndexPath:indexPath];
Person_ *thisPerson;
if(tableView == self.searchDisplayController.searchResultsTableView)
{
thisPerson = (Person_ *) searchResults[indexPath.row];
NSLog(#"searchResultsTableView, %#",thisPerson.sName);
}
else
{
thisPerson = (Person_ *) people[indexPath.row];
NSLog(#"UITableView, %#",thisPerson.sName);
}
Person_ *thisSpouse = [[DAL sharedInstance] getSpouse:thisPerson People:people];
// cell.fieldName and cell.fieldValue are nil, cell is not nil
cell.fieldName.text = thisPerson.sName;
cell.fieldValue.text = thisSpouse.sName;
return cell;
}
GenericDetailCell.h:
#interface GenericDetailCell : UITableViewCell
#property (nonatomic,weak) IBOutlet UILabel *fieldName;
#property (nonatomic,weak) IBOutlet UILabel *fieldValue;
#end
Seems to me you might be attempting to combine a UITableViewCell predefined style (e.g. UITableViewCellStyleSubtitle) with a custom cell.
In your storyboard, if you have selected a predefined style for your Prototype cell, other controls will not be recognised.
To remedy this problem, in the Attribute Inspector for your Prototype UITableViewCell, select "Custom" type. Then add into the Prototype cell all the controls you require, including those needed to replace the default controls previously added automatically into the predefined cell type.
Related
How do I get tableView by custom cell in the CustomCell?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell" forIndexPath:indexPath];
return cell;
}
#implementation CustomCell
- (void)awakeFromNib {
[super awakeFromNib];
// How do I get tableView by custom cell in the CustomCell?
}
#end
To answer the question, Apple does not provide a public API for that and you would have to do it using what is known about view hierarchies.
A tableViewCell would always be part of a tableView. Technically, a tableViewCell would always be in a tableView's view hierarchy or a tableViewCell would always have some superview out there that is a tableView. Here is a method similar to this one:
- (UITableView *)getParentTableView:(UITableViewCell *)cell {
UIView *parent = cell.superview;
while ( ![parent isKindOfClass:[UITableView class]] && parent.superview){
parent = parent.superview;
}
if ([parent isKindOfClass:[UITableView class]]){
UITableView *tableView = (UITableView *) parent;
return tableView;
} else {
// This should not be reached unless really you do bad practice (like creating the cell with [[UITableView alloc] init])
// This means that the cell is not part of a tableView's view hierarchy
// #throw NSInternalInconsistencyException
return nil;
}
}
More generally, Apple did not provide such public API for a reason. It is indeed best practice for the cell to use other mechanisms to avoid querying the tableView, like using properties that can be configured at runtime by the user of the class in tableView:cellForRowAtIndexPath:.
In my application, I have done Expanded table view cell inside that again I need to expand the cell.I Am facing the problem with again inserting expanded cell.Is it possible to do?
//cellForRowAtIndexPath:
[cell.expandButton addTarget:self action:#selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.item < [array count] + 1) {
NSLog(#"Show one cell”);
}else{
NSLog(#"Show another cell")
}
//Button action
-(void) expandAction:(UIButton*)sender{
if (indexPath.item < [array count] + 1) {
NSLog(#"Show one cell”);
}else{
NSLog(#"Show another cell")
}
}
How do you generate your tableView data? If you use array of objects, then you could make it kind of like that:
CellItem class saves cell state
#interface CellItem : NSObject
#property BOOL isExpanded;
#end
Your custom cell class. Setup method customize your cell depending on CellItem object state
#interface ExpandableCell : UITableViewCell
- (void)setupWith:(CellItem *)cellItem;
#end
#implementation ExpandableCell
- (void)setupWith:(CellItem *)cellItem {
if(cellItem.isExpanded) {
//Expand your cell
} else {
//Don't exp and
}
}
#end
Array of objects where you get data for your tableView
#property NSArray<CellItem *> *items;
Protocol, which defines methods for cell state delegate. Implement this methods in class, which is also delegate of yourtableView. Call this method from cell everytime user expand / close it. In implementation save cellItem in items arrays.
#protocol ExpandableCellDelegate
- (void)cellDidChangeStateWithCellItem: (CellItem *)cellItem;
#end
UITableView cells are reusable, so everytime cell appears on a screen you should customize it again.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellItem *cellItem = self.items[indexPath.row];
ExpandableCell *cell = (ExpandableCell *)[tableView dequeueReusableCellWithIdentifier:#"InsertYourCEllID"];
[cell setupWith:cellItem];
return cell;
}
I have a UITableView tall enough that it necessitates scrolling. The top-most cell in the table contains a UITextField for the user to enter some text.
The standard way to build this might be to create and add the text field and add it to a cell created or recycled in cellFOrRowAtIndexPath: However, this constant re-creation means that the text entered in the field is erased when the cell is scrolled out and back into view.
The solutions I've found so far suggest using UITextField delegation to track the text as it changes and store it in an iVar or property. I would like to know why this is recommended instead of the simpler approach I am using:
I am creating the UITextField in the init method of the UITableViewController and immediately storing it in a property. In cellFOrROwAtIndexPath I am simply adding the pre-existing field instead of initializing a new one. The cell itself can be recycled without issue, but because I am always using the one and only UITextField, the content is maintained.
Is this a reasonable approach? What might go wrong? Any improvements (perhaps I could still create the field in cellForRowAtIndexPath but first check if the property is nil?)
When you are creating cells in cellForRowAtIndexPath you have to use one reusable identifier for that first cell (ie. cellId1) and another for the rest (ie. cellId2).
If you do this, when you get the cell for the first element by calling [tableView dequeueReusableCellWithIdentifier:#"cellId1"] you will always get the same Object and will not be reused by other cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = nil;
// Only for first row
if (indexPath.row == 0) {
static NSString *cellId1 = #"cellId1";
cell = [tableView dequeueReusableCellWithIdentifier:cellId1];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId1];
}
}
else {
static NSString *cellId2 = #"cellId2";
cell = [tableView cellId2];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault cellId2];
}
}
// do whatever
return cell;
}
If there is only one UITextField, then I agree that your approach would be better/same as compared to using UITextField delegation (I think).
However, let us assume that you want to "expand" your view so that there are about 7-8 or more TextFields now. Then if you go about using your approach, then the problem will be that you will be storing 7-8 or more TextFields in memory and maintaining them.
In such a situation, a better approach would be that you create only that number of textfields as visible on screen. Then you create a dictionary which would maintain the content present in the textfield (which you can get by UITextFieldDelegate methods). This way, the same textfield can be used when the cell is reused. Only the values will change and will be dictated by the values in the dictionary.
On a sidenote, do minimal creation in cellForRowAtIndexPath as that is called during every table scroll and so creating a textField in cellForRowAtIndexPath can be expensive.
#import "ViewController.h"
#import "TxtFieldCell.h"
#define NUMBER_OF_ROWS 26
#interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tablView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tablView.datasource = self; //set textfield delegate in storyboard
textFieldValuesArray = [[NSMutableArray alloc] init];
for(int i=0; i<NUMBER_OF_ROWS; i++){
[textFieldValuesArray addObject:#""];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - TableView Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TxtFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TxtFieldCellId" forIndexPath:indexPath];
cell.txtField.tag = indexPath.row;
if (textFieldValuesArray.count > 0) {
NSString *strText = [textFieldValuesArray objectAtIndex:indexPath.row];
cell.txtField.text = strText;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return NUMBER_OF_ROWS;
}
#pragma mark - TextField Delegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textFieldValuesArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}
I have an nslog that is successfully telling me that the information I will load into dynamic cells is there. However, I've discovered that my tableView is not loading the array "dogs" with object "dogs" at all because the nslog for name11Label is returning null despite the fact that there is a value for dogs in viewDidLoad. What would I need to do to initiate the tableView? (.h does have an iboutlet and a property(just to make sure) for my "tableView" and also has as well as and import for cell11.h)
.m file
-(void)viewDidLoad{
...
self.tableview.delegate = self;
...
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
if ( dogs != NULL ) {
return [dogs count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Cell11 *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell11"];
if (cell == nil)
{
cell = [[[Cell11 alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell11"] autorelease]; //I know that this method is depreciated, but it is not the source of this problem
}
NSDictionary *itemAtIndex =(NSDictionary *)[dogs objectAtIndex:indexPath.row];
cell.name11Label.text = [itemAtIndex objectForKey:#"dogs"];
NSLog(#"dogs array = %#", dogs); //returns correct information of the object dogs
NSLog(#"%#", cell.name11Label.text); //returning null even though "dogs" in viewDidLoad is showing a result;
return cell;
}
Cell11.h
#import <UIKit/UIKit.h>
#interface Cell11 : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *name11Label;
#end
Cell11.m
#import "Cell11.h"
#implementation Cell11
#synthesize name11Label;
#end
Change:
Cell11 *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell11"];
to
Cell11 *cell = [tv dequeueReusableCellWithIdentifier:#"Cell11"];
tableView is probably not hooked up right.
The nslog statement will never get called. Move it above:
NSLog(#"%#", cell.name11Label);
return cell;
Also you are initializing the cell with cgrect zero which won't display anything. Make sure it has a height and width that are expecting (e.g. CGRectMake(0.0f, 0.0f, 320.0f, 50.0f))
If the cell still doesn't display, are you calling reloadData on the tableview after the array dogs is set up?
Have you assigned your UITableView's "dataSource" property to the object with the UITableViewDataSource protocol (the above methods)?
First of all: The NSLog after the return will never be called.
NSLog(#"%#", cell.name11Label);
return cell;
}
Second: Control drag from you tableview of the interface builder to your class. And make sure the connection dataSource is properly connected.
In my iPad app storyboard design, there are two table views added.
One table view is for displaying the folder names and the other table view is for displaying the file names. When ever a folder table view cell is selected the files inside the selected folder needs to to be displayed in the other table view(the files table view).
My problem is
I am confused about
How to add delegate and data source for each table view in ViewController ? or is it possible to add data source and delegates for each table view in a custom class other than ViewController ?
and
How to handle the selection of cell ?
Please help !
First off: Why don't you just display the files on a pushed viewController which takes up the whole screen? Seems more intuitive for me.
If you want to do it with two tableViews, and assuming they use dynamic cells than:
1, In the .h file of your view controller
specify two tableView properties like so:
#property (weak, nonatomic) IBOutlet UITableView *foldersTableView;
#property (weak, nonatomic) IBOutlet UITableView *filesTableView;
implement the two delegate protocols of the UITableVIew
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
2, Add the two UITableViews onto your ViewController, and...
...link your Outlets to the two tableviews
...on the attributes inspector set the Tag of the foldersTableView to 1 and the filesTableView to 2
...select each of the UITableViews, go to the Connections Inspector and link their two delegate methods (delegate and datasource) to your ViewController (for both of them)
3, In the .m file of your ViewController implement the three datasource methods of UITableView like so:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView.tag == 1) {
return theNumberOfSectionsYouWantForTheFolderTableView;
} else {
return theNumberOfSectionsYouWantForTheFilesTableView;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView.tag == 1) {
return [foldersArray count];
} else {
return [filesArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
}
4, Implement the selection:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
//fill filesArray with the objects you want to display in the filesTableView...
[filesTableView reloaddata];
} else {
//do something with the selected file
}
}
Hope I got everything correctly. Don't forget to #synthesize the properties in the .m file if you are using pre XCode 4.4.
If you are using multiple tables, don't forget to hide as appropriate:
- (void)viewWillAppear:(BOOL)animated
{
[self.table2 setHidden:YES];
[self.table1 setHidden:NO];
// Probably reverse these in didSelectRow
}