Can I use UITableviewCell like this? - iphone

Normally in a UITableView, we assign the value for the cells in the cellForRowAtIndexPath method which is the delegate method of UITableView. In this case, if we use a CustomTableViewCell, we assign the values in cellForRowAtIndexPath as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = customTableViewCell;
}
cell.label1.text = #"text1";
cell.label2.text = #"text2";
// Configure the cell...
return cell;
}
This way of using CustomCell is a usual one. The texts will be displayed on the labels of the cell, but I need some clarification in using CustomCell in another way. I have a method in MainViewController like the following
- (void)methodInMainViewConroller {
CustomCell *customCell = [[CustomCell alloc] init];
[customCell methodInCustomCell];
[tableView reloadData];
}
and in CustomCell, I have the definition for the method as
- (void)methodInCustomCell {
self.label1.text = #"text1";
self.label2.text = #"text2";
}
Will this work? Will the text be displayed on the labels of the cell? Actually it doesn't work for me. Anyone help me to make this work perfectly. Thanks in advance.

You can try this
- (void)methodInMainViewConroller {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CustomCell *customCell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];
}
UPDATE: What I've just realized is that once you scroll the table view your labels will be reset to previous values. It might not work this way, but give it a try.

Take a good look at your code. You never actually do anything with that cell once you set the labels. Anyway, how would the tableview know to use your cell and where to put it?

It doesn't work because the cell you alloc/init and work on is a totally different object. You can, however, store that cell as an ivar and always return that very cell for the given indexPath.

This method don't work because you call it for just created custom cell not for cell in tableView.
You need something like this:
CustomCell *customCell = [tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];

Related

setting UILabel.text of a custom UITableViewCell inside ViewController

I have a UITableView that I am populating with custom UITableViewCell in interface builder. I am having some issues accessing these custom tableviewcells properties and am hoping for some help.
In Interface Builder I am setting the custom tableviewcell's class to the current View controller (so I can assign all of the label objects to the correct labels in Interface Builder), So I have also set up the IBOutlet labels to the correct labels in Interface Builder However this error occurs when I try to pass the NSString from the array object variable (which is of type NSString) to the UIlabel's text.
Property 'cellDescription' not found on object of type 'UITableViewCell *'
Below is the code I have used to set up my tableview with the custom tableviewcell and then try to populate the cells UILabels with the correct text..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
// Set cell height
[self tableView:tableView heightForRowAtIndexPath:indexPath];
// Configure the cell using custom cell
[[NSBundle mainBundle] loadNibNamed:#"AutomotiveSearchCell" owner:self options:nil];
cell = autoSearchCell;
//call dataArrayOfObject that has all of the values you have to apply to the custom tableviewcell
SearchResultItem* myObj = (SearchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];
cell.cellDescription.text = myObj.seriesDescription; // This is where I am receiving the error
NSLog(#"%#", myObj.seriesDescription); // This logs the correct value
//Disclosure Indicator
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
you must typeCasting UITableViewCell To AutomotiveSearchCell
I think you code somewhere is strange(There is no declaration for autoSearchCell), but you must do the following.
cell = (AutomotiveSerachCell* )autoSearchCell;
The above code does not work, should following code.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
convert to
AutomotiveSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
If that does not work above, refer a following process.
make a CustomCell Class.
make a CustomCell xib.
linked label to CustomCell Class.
import header #import "AutomotiveSearchCell.h" and following code copy and paste.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutomotiveSearchCell *cell = nil;
if(!cell)
{
UINib *nib = [UINib nibWithNibName:#"AutomotiveSearchCell" bundle:nil];
NSArray *arr = [nib instantiateWithOwner:nil options:nil];
cell = [arr objectAtIndex:0];
cell.cellDescription.text = #"Test~!~!~!";
}
// Configure the cell...
return cell;
}

No text in my UITableViewCell

just created an UITableView,
trying to display some custom data from an array, but what ever I do, i get no text displayed.
NSLog tell me the right text and right amout but no text in table cell.
here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Things to check:
Is cellForRowAtIndexPath getting called? Put a breakpoint in
Make an array in ViewDidLoad (property, alloc'd) and addObjects #"One", #"Two", #"Three", nil and then cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
IfcellForRow is being called, this will show in the cells.
What doesConfigureCell do? Include the code please.
Also check your tableView delegate methods are being called (NumberofRowsInSection etc)
Always set cell properties (text, images, accessory views, etc) in the
tableView:willDisplayCell:forRowAtIndexPath
delegate method.
UITableView sometimes send a prepareForReuse to the cell after it is returned from
tableView:cellForRowAtIndexPath
which causes the cell to reset it's labels and images.

dequeueReusableCellWithIdentifier not working with ARC and IOS 4

I can't seem to get dequeueReusableCellWithIdentifier working.
I need to build a project for IOS 4 so I can't use storyboards, but I'm using ARC.
Let's say I have 2 sections, each with 1 row.
Looking at the code below, I'm using the strong property to pass ownership, since ARC would insert the "autorelease" code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TableCellIdentifier";
MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
self.retainedCell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
However, for each row, cell is always nil (and hence a new MainTableCell is alloc'd) each time the function is called. The cell is never re-used.
This wouldn't be so much a problem except I programmatically call tableView:cellForRowAtIndexPath:, meaning I get a newly alloc'd cell each time, rather than the existing cell.
The only method I can see is to add the cells to an NSMutableArray.
Is there something I'm missing with dequeueReusableCellWithIdentifier now?
Thanks!
EDIT
I'm using the code below to get the cell. As mentioned it's creating a new cell not re-using the one that should have been already made + retained.
I don't need to invoke reloadData for all the rows, just change a specific one.
MainTableCell *cell = (MainTableCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self configureCell:cell atIndexPath:indexPath];
You happen to be de-queuing MainTableCell, and then you proceed to check if it is nil, at which point you use a completely different var to alloc a table cell. What the heck? Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TableCellIdentifier";
MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

dequeueReusableCellWithIdentifier not returning cell of my custom type

I am using ios5 storyboards with a UITableViewController with a UITableViewCell subclass. I do not want to design the visual elements of the cell in the storyboard designer for the view, because I want to use a reusable subclass of UITableViewCell (specifically TDBadgedCell).
I have set my cell identifier in the storyboard designer, and all of the rows load correctly in the UITableView as long as I'm not setting any of the properties unique to TDBadgedCell. If I set the badgeString property though which is unique to TDBadgedCell, I get an exception. I narrowed down that dequeueReusableCellWithIdentifier: is not returning a cell of type TDBadgedCell.
I'm only running into this with a UITableViewController. I have a UIViewController with an embedded UITableView set up in the same fashion and it's not an issue. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"PhoneNumberCell";
TDBadgedCell *cell = (TDBadgedCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([cell isKindOfClass:[TDBadgedCell class]])
{
NSLog(#"It is TDBadgedCell");
}
else
NSLog(#"It is NOT TDBadgedCell");
I had a similar issue in that I am subclassing UITableViewCell but not using storyboard. Here is my solution to using different cell classes dependent on if the user had purchase the unlock feature of the app. Hope it helps someone.
In a nutshell, I had the cell with several objects including a UITextView object. I wanted to lock down the copy and paste feature of the UITextView object in the lite version but then release the feature once the user had purchased in in-app product.
I had two UITableViewCell classes, one with UITextView as it is and another with UITextView subclassed with canBecomeFirstresponder returning NO. That way the user could still scroll up and down the UITextview data but not copy and paste the data.
Here is the code and all I had to do was rename the reuse identifiers.
WHY? Because [self.tableview reloadData] would not rebuild the cells with the new class as the cell was still in existence. New cells off the screen would get the new class but existing ones would not. This solution rebuilds all cells once off after the purchase unlocking the added feature.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (your test if in-app was purchased is yes)
{
static NSString *MyIdentifier = #"MyCell";
FrontCell *cell = (FrontCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[FrontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.shouldIndentWhileEditing = NO;
}
//....///
cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;
return cell;
}
else // inapp not purchased
{
static NSString *MyLockedIdentifier = #"MyLockedCell";
FrontCellLocked *cell = (FrontCellLocked *)[tableView dequeueReusableCellWithIdentifier:MyLockedIdentifier];
if (cell == nil)
{
cell = [[FrontCellLocked alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyLockedIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.shouldIndentWhileEditing = NO;
}
//....///
cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;
return cell; }
}
In storyboard,you can set the Custom Class property for a subclass of UITablviewCell.
Then dequeueReusableCellWithIdentifier method will return cell with the type of your subclass.
I think you are using wrong method for dequeuing the cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tblProfileInfo dequeueReusableCellWithIdentifier:#"PostCell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
You forgot for indexPath at the end.

UITableView section details drawing wrong on scroll

I have a UITableView, and I am showng data based on indexPath.section, however, when I scroll my table view very quickly, its data keep overlapping. How to fix this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
// case 0 to 10;
//values change if I scroll my table
}
}
I think you are allocating some labels inside the cellForRowAtIndex delegate method. Thats why you got this problem. You can solve this in 2 ways:
Alloc the labels outside the delegate
method. set tags for your labels and
use it inside the cellForRowAtIndex
by refering the tags.
use custom cell view controller.
i fixed the issues
here is the solution if any one need
NSString *CellIdentifier=nil ;
NSMutableArray *Array= [[[NSMutableArray alloc] initWithObjects: #"One",#"Two", #"Three",#"Ad",#"Ae",#"Ah",#"Aj" ,nil]autorelease];
CellIdentifier = [Array objectAtIndex:indexPath.section];
///302-1021-9244-4658-1994-3384
UITableViewCell * cell = [tabelView dequeueReusableCellWithIdentifier:CellIdentifier];
Thanks