Uilabel and a Tablecell - objective-c

A small issue i created a custom UITablecell but in the cell it's need to parse data so i connected IBOutlet UILabel *One; to an UILabel but when i'm doing
One.text = #"Lorem..."; an error show's up i imported the UITablecell.h in mijn viewController.
**Use of undeclared identifier 'One'**
/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"ViewControllerCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (ViewControllerCell*)view;
}
}
}
One.text = #"Lorem...";
return cell;
}

In this case your instance of your custom UITableViewCell class will be cell, so you'll need to access it like this
cell.One.text = #"Lorem..";

First you have to Typecast the tableViewCell to your Custom Cell.
{
YourCustomCell *objCustomCell=(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPathForThatRow];
objCustomCell.One.text=#"YourDesiredstringvalue";
}

Related

"no index path for table cell being reused" issue for custom cell

I am having a Tableview within that i have a custom cell and within that i have another tableview. In this cellForRowAtIndexPath method i am getting this error no index path for table cell being reused and cell gets disappeared after scroll. Is this indexpath issue or cellidentifier issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// __block CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (customCell == nil)
// {
// customCell = [[[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)] autorelease];
//
// }
[customCell prepareCell:arrCategory];
return customCell;
}
-(void)prepareCell:(NSArray *)arrCategory
{
if(mutArr != nil) {
[mutArr removeAllObjects];
mutArr = nil;
[mutArr release];
}
mutArr = [[NSMutableArray alloc] arrCategory];
[tblCusom reloadData];
}
I go through this SO ques but i am not using the method used in this question. So what could be the problem I am not able to trace it. Also not found the same issue after googling
If I right understood, you need check tableView (mainTableView or tblCustom) for which call method of delegate.
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == tblCusom)
{
__block CustomCell *categoryCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (customCell == nil)
{
customCell = [[[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)] autorelease];
}
[customCell prepareCell:arrCategory];
}
else if (tableView == self.myMainTableView)
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return categoryCell;
}
return customCell;
}
Hope it helps you.
I was also facing this bug from a long time and found a solution :-
[tblCusom reloadData];
Replace it with
[self performSelector:#selector(tableReloadMethod) withObject:nil afterDelay:0.5];
Add this method:-
-(void)tableReloadMethod
{
[tblCusom reloadData];
}
Hope it helps you.
CustomCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
You are creating a cell instance but not using it anywhere,and that cell is returning from the reuseIdentifier method.So in fact no use of this line in your code

Issue when getting data from text field in dynamic cell

I have created a dynamic table, but I'm facing an issue when I try to access the content of a text field in the first cell. The textfield I get is always empty, even though I enter some text in it before calling the action.
Here is my code. Could you help me to find what's wrong in it ?
CustomNameCell.h
#interface CustomNameCell : UITableViewCell
#property (retain, nonatomic) IBOutlet UITextField *nameTextField;
#end
CustomViewController.h
#interface CustomViewController : UITableViewController
#property (retain, nonatomic) IBOutlet UITableView *tableview;
- (IBAction)search:(id)sender;
#end
CustomViewController.m :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[CustomNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
...
and the action when clicking a button, where I try to get the content of the textfield :
- (IBAction)search:(id)sender {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *nameCell = [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];
here nameCell.nameTextField.text is always equal to #""
Why is my text field always empty ?
Thank you for your help ;)
If you need a cell you should get it like this:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:indexPath];
NSString * textInField = cell.nameTextField.text;
If you tell me what logic do you need I'll expand my question. For now - you can use this method.
Content of the first cell will be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * textInField = cell.nameTextField.text;
You shouldn't call [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];within your - (IBAction)search:(id)sender as it is a mechanism to reuse a cell during the creation of your tableView. Instead of creating new cell when you are scrolling your tableview you are reusing some cells you have previously created.
Bottom line: use instead the cellForRowAtIndexPath to access a cell at a specific row in your section and if you want the first cell of the first section it would be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
I just do in this Way. I dont know it is the current way to do that.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellContentArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITextField *firsttext=[cell viewWithTag:101];
UITextField *second=[cell viewWithTag:102];
[second setTag:(indexPath.row)+1000];
[firsttext setTag:(indexPath.row)+100];
return cell;
}
- (IBAction)buttonPressed:(id)sender
{
for (int i=0; i<cellContentArray.count; i++) {
UITableViewCell * cell = (UITableViewCell *)[baseTableveiew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UITextField *tempTextField=[cell viewWithTag:i+100];
UITextField *secondTextField=[cell viewWithTag:i+1000];
NSString * textInField = tempTextField.text;
NSLog(#"tempTExtTiedl tag %ld data %#",(long)tempTextField.tag, textInField);
NSLog(#"second tag %ld data %#",(long)secondTextField.tag, secondTextField.text);
}
}

iOS 5, Storyboards, ARC: UITableview doesn't get populated with data from custom delegate and datasource

This is my first time I am working on UITableView connected to custom delegate and datasource. Until today, I used to connect to "self". The prequel for this question is here.
So, I have two additional UIViews. Using segmented control I put one of the over mentioned UIViews on self.view...
Have created two UITableView subclasses and set them as delegate and datasource. Works fine, no leaks or crashes. Checked if the classes getting initialized on segmentedControl index change:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
NSLog(#"Nominals got init");
}
return self;
}
NSLog works when changing segment.
The question:
In my custom delegate class I override the methods required by UITableViewDelegate and UITableViewDataSource protocols.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
At moment I am getting exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Well, isn't the cell in my custom delegated class is the same cell of my UITableView I've created:
-(void)populateNominals:(int)subCountryID
{
NominalsTableViewDelegate *del=[[NominalsTableViewDelegate alloc]init];
[self setNominalsDelegate:del];
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
[nominalsTableView setDelegate:nominalsDelegate];
[nominalsTableView setDataSource:nominalsDelegate];
[nominalsTableView reloadData];
[self.nominalsView addSubview:nominalsTableView];
}
what's my mistake? Thank you in advance.
You aretrying to dequeue a cell using dequeueReusableCellWithIdentifier. If there is no cell in the queue what happens ? Definitely crash.
So you need to allocate a cell, add this line too:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] init];
}
On your tableView:cellForRowAtIndexPath: method, you are missing the case where there are no cells to be dequeued. Take a look at your code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
You should do something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] init];
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=#"some nominal";
cell.textLabel.text=#"Nominal";
[cell addSubview:lbl];
return cell;
}
because by doing that, if you can't dequeue a cell, you're creating a new one.
the dequeueReusableCellWithIdentifier will return UITableViewCell instances when the cells are being reused while scrolling through the tableView.
So add
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
You have to use the initWithStyle:reuseIdentifier: which is the designated initializer for tableview cells.

Return a custom cell from cellForRowAtIndexPath:

I ran into a trouble and I'm looking for help.
I need to save a identifier in a cell, so I did a subclass
of UITableViewCell and added to it the identifier property. In my view
controller I create the cells as follow:
- (SidebarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
SidebarCell * cell = (SidebarCell *)tableViewCell;
Category *category = [categoryDataController.categories objectAtIndex:indexPath.row];
cell.textLabel.text = category.name;
if (category.checked == 1) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
The problem is that when I select one of the cells, the method
cellForRowAtIndexPath: returns an UTableViewCell object instead
of a SidebarCell, so I don't have access to the identifier property:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; // Trying to cast from UITableViewCell to SidebarCell
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[CategoryDataController updateRow:cell.identifier status:1]; // Here the app crashes
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[CategoryDataController updateRow:cell.identifier status:0];
}
}
is there a way for cellForRowAtIndexPath: to return an Sidebarcell object?
Thanks beforehand.
Edit
The SidebarCell.h
#import <UIKit/UIKit.h>
#interface SidebarCell : UITableViewCell
#property (nonatomic) NSInteger identifier;
#end
SidebarCell.m:
#import "SidebarCell.h"
#implementation SidebarCell
#synthesize identifier;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
The error:
2012-04-24 09:21:08.543 Dongo[2993:11603] -[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50
2012-04-24 09:21:08.571 Dongo[2993:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50'
*** First throw call stack:
(0x14b1052 0x1a65d0a 0x14b2ced 0x1417f00 0x1417ce2 0x116bc 0x48e71d 0x48e952 0xd1686d 0x1485966 0x1485407 0x13e87c0 0x13e7db4 0x13e7ccb 0x239d879 0x239d93e 0x3fea9b 0x2e85 0x2715 0x1)
terminate called throwing an exception(lldb)
You should leave the tableView:cellForRowAtIndexPath: method signature intact: it should return (UITableViewCell *) and not your custom cell class. Since your SidebarCell is a subclass of UITableViewCell, it should all "just work". No need to call super or any of that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[SidebarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"CellIdentifier#"];
}
// ... set up the cell here ...
return cell;
}
In other methods, the cast should work just as you have it:
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath];
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
The above line of code must be returning a UITableViewCell and not a SidebarCell.
SidebarCell * cell = (SidebarCell *)tableViewCell;
Then on your second line you cast the cell to a SidebarCell, but you are simply lying to the compiler and telling it that it should be a SidebarCell when it is in fact not. When you cast something it needs to start out as the correct type.
Instead of those two lines, you should be creating an actual SidebarCell. How you do this depends on how you have defined it.
Let me know if it is in a storyboard, xib, or created in code and I can provide an example if needed.
EDIT
Since you are creating the cell in code, you simply need to replace the first two lines in cellForRowAtIndexPath with this:
SidebarCell * cell = [[SidebarCell alloc] init];
Everything else looks like it should work as-is.
In my case i have used custom cell of a class tempTableViewcell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"cellidentifier";
tempTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = #"abc";
return cell;
}

Table view doesn't show up custom cells

I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Many thanks
Why are you creating your cell like this?
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If it is your own custom cell why do you use UITableViewCellStyleDefault ?
If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.
In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"nibName" owner:nil options:nil];
for (alrededorCell *view in xib) {
if ([view isKindOfClass:[alrededorCell class]]) {
cell = view;
}
}
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}