cellForRowAtIndexPath returning custom cell? - iphone

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
PlanetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PlanetCell_ID"];
return cell;
}
If your creating a custom UITableViewCell (in this case PlanetTableViewCell) is it acceptable to return that object via a method returning (UITableViewCell *), or is there something else I should be doing?

If your creating a custom UITableViewCell (in this case PlanetTableViewCell) is it acceptable to return that object via a method returning (UITableView *), or is there something else I should be doing?
You possibly meant:
to return that object via a method returning (UITableViewCell*),
If so, then it is perfectly legal and reasonable.
Indeed, your PlanetTableViewCell being derived from UITableViewCell, all instances of PlanetTableViewCell are also of the type UITableViewCell (is-a relationship in OOP).

Yes, this is the correct way to return the cell.
But you should also be checking to see if your "dequeue" is returning a valid cell object. If not, you'll need to create one.
This method is also where you should be configuring your cell with title, accessories, etc.
Sample code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
PlanetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PlanetCell_ID"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.titleLabel.text = #"Cell Title";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

Using custom cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
simpleTableIdentifier = #"dashboard_logintimeCell_ipad";
}
else
{
simpleTableIdentifier = #"dashboard_logintimeCell";
}
dashboard_logintimeCell *cell = (dashboard_logintimeCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib =[[NSBundle mainBundle]loadNibNamed:simpleTableIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
/*here you cell object get
like
cell.lable.text=#"yourlabeltext";
*/
cell.backgroundColor=[UIColor clearColor];
return cell;
}

Related

Error returning cell from tableView

I am relatively new to using table view in ios. I am trying to edit data using different view and update the values from original view. I set cell identifier and wrote following code.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}
But I get the following error:
NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
You need to check and make sure dequeueReusableCellWithIdentifier was able to dequeue a cell. It's crashing because it doesn't return a cell every time. If you were unable to dequeue a reusable cell you need to create a new one. Your code should look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}

Unable to Populate TableView In PopOverController - Objective C

I have a UIButton called as UploadButton. I am using the following lines of code which takes care of the action which should happen on clicking it ::
-(IBAction)UploadButtonPressed:(id)sender{
self.Upload = [[UploadSpaceTableViewController alloc]
initWithStyle:UITableViewStylePlain];
self.UploadTableViewPopover = [[UIPopoverController alloc]
initWithContentViewController:Upload];
[self.UploadTableViewPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Here, UploadSpaceTableViewController is a separate class which I have made. It has the following functions in it ::
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
self.keys1 = [NSMutableArray array];
[keys1 addObject:#"Red"];
[keys1 addObject:#"Green"];
[keys1 addObject:#"Blue"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [keys1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
Basically, all I want is to display a UItableView inside a UIPopOverController on the click of my UploadButton.
However, on running the above lines of code I am getting the following erre in gdb ::
splitView[4486:f803] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-06-27 14:05:05.531 splitView[4486:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
This is the first time that I am trying to display a UITableView inside a UIPopoverController. I have tried a lot of variations in the code but, I am unable to sort it out. Can someone help me out ?? Thanks and regards.
Have you allocated your cell i think by your code it will return nill
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nill){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
may this will help you...
That error means that you're passing an invalid argument when you're assigning a value to keys1, self.keys1 = .... If you're, e.g., passing a NSMutableArray to an NSDictionary property, you'll get that error.
The problem is the following line:
self.keys1 = [NSMutableArray array];
Your UploadSpaceTableViewController has no method setKeys / no property keys.
Edit for the 2nd error message:
UITableView dataSource must return a cell from
tableView:cellForRowAtIndexPath:
This means that you should implement the UITableViewDataSource method tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//configure your cell here
return myCongiuredTableViewCell;
}
Use the following code in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
I think this may helpful for you.

Loading a UITableViewCell subclass using a XIB file

I am having trouble getting my CustomTableViewCell, a subclass of UITableViewCell to appear in my table view.
I am using a xib to represent that cell, but I am assuming that the code for the data source delegate doesn't change. I made sure to set an identical reuse identifier inside the table view cell XIB.
I isolated the problem to the fact that the datasource method that returns the table cell isn't working correctly, here it is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
DataObject *foo = [self.dataArray objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell overview] setText:foo.overview];
[[cell price] setText:foo.price];
NSLog(#"cell initialized, description text is %#",cell.overview.text);
return cell;
}
Not sure why this isn't working, but that last log statement always prints a (null) at the end, and yes I did verify that the overview property of the data object has a valid string in it. Same thing for price.
1) Make your Custom cell class separate
2) XIB : File Owner class change to NSObject
3) XIB : UITableViewCell change to MyTableViewCell
4) Where you want to add inside table view code as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyCell";
MyTableViewCell *cell = (MyTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell"
owner:self
options:nil];
if (indexPath.row == 0) {
// first table cell
cell = [arrayCellXib objectAtIndex:0]; // *
......
}else {
cell = [arrayCellXib objectAtIndex:1]; // *
}
...
}
return cell;
}
Object at index 0,1 ... are index for which order you make MyTableViewCell in xib, if you want to have more than one custom cell. means in a MyTableViewCell class u make unlimited custom cell and use according to requirement.
Normally, just make ONE custom cell in the XIB and do exactly this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int thisRow = indexPath.row;
NSString *exampleText = [yourData objectAtIndex:thisRow];
static NSString *CellID = #"cu5";
FancyCell *cell =
(FancyCell *)[tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil)
{
NSArray *cellTeam =
[[NSBundle mainBundle] loadNibNamed:#"FancyCell"
owner:self options:nil];
cell = [cellTeam objectAtIndex:0];
}
[cell someFunction:exampleText];
[cell.someLabel setText:exampleText];
return cell;
}
Hoping may help u :)
In .h file, you write it.
IBOulet CustomTableViewCell *tableCell;
and connect to File's Owner in CustomTableViewCell's .xib file.
You modify this in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = tableCell;
}
I think it will be helpful to you.

Dynamically assigning image to CustomCell

I have different texts in my custom cells in my UITableView, each text has a category. according to category a certain image is assigned to UIImageView in my CustomCell like that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CCell";
// Dequeue or create a cell of the appropriate type.
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell.
cell.primaryLabel.text = [self.arrayWithTextsFromSelectedCategory objectAtIndex: indexPath.row];
NSString *keyForText=[self.arrayWithTextsFromSelectedCategory objectAtIndex:indexPath.row];
categoryForPicture=[dicWithTitlesAsKeysAndCategoriesAsValues objectForKey:keyForText];
//cell.categoryImg=[dicWithTitlesAsKeysAndCategoriesAsValues objectForKey:keyForText];
NSLog(#"Current category!!! %#", categoryForPicture);
NSLog(#" %# ", keyForText);
//NSString *textToExtract;
//textToExtract=[self.texts objectForKey:keyForText];
cell.secondaryLabel.text=[self.texts objectForKey:keyForText];
if (categoryForPicture==#"Стихи")
{
NSLog(#"Assigning proper image!!");
cell.iconImage.image=[UIImage imageNamed:#"stixi.png"];
}
if (categoryForPicture==#"Анекдоты")
{
NSLog(#"Assigning proper image!!");
cell.iconImage.image=[UIImage imageNamed:#"jokes.png"];
}
else
{
cell.iconImage.image=[UIImage imageNamed:#"stand_icon.png"];
}
//cell.imageView.image=[UIImage imageNamed:#"cell.png"];
//cell.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"cell.png"]]];
return cell;
}
still doesn't work... any ideas? thanks!
the problem is your string equality check...this categoryForPicture==#"Анекдоты" will return true only if the two are the same object, which will never be the case here, you want to us NSStrings isEqualToString: in order to accomplish your goal... so
[categoryForPicture isEqualToString:#""]
hope that helps

Problem in displaying custom UITableViewCell

I have a tableview displaying data using a custom UITableViewCell(say cell1). On touch of a row, I change the custom cell to another custom UITableViewCell(say cell2). Also I increase the size of row of the selected cell to accomodate, more items of cell2.
My problem is that the approach works fine when the number of rows in table view is less. When the number of rows increses, the rows expand on touching, but the data displayed is same as that of cell1. Cell2 doesn't seem to be loaded at all.
Looking forward for a reply soon....Thanks in advance.
I am posting my code....please let me know if there is a problem in that.
My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath == selectedCellIndexPath)
{
static NSString *Identifier1 = #"Identifier1";
Identifier1 = #"cell2";
CustomCellController1 *cell = (CustomCellController1 *)[tableView dequeueReusableCellWithIdentifier:Identifier1];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCellController1View" owner:self options:nil];
cell = rowTwoObject;
}
[cell setText:[test1Array objectAtIndex:indexPath.row]];
return cell;
}
else {
static NSString *Identifier2 = #"Identifier2";
Identifier2 = #"cell1";
CustomCellController1 *cell = (CustomCellController1 *)[tableView dequeueReusableCellWithIdentifier:Identifier2];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCellController1View" owner:self options:nil];
cell = rowOneObject;
}
[cell setText1:[temp1Array objectAtIndex:indexPath.row]];
[cell setText2:[temp2Array objectAtIndex:indexPath.row]];
return cell;
}
}
didSelectRowAtIndexPath looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCellIndexPath = indexPath;
[tableView reloadData];
}
heightForRowAtIndexPath looks like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 110;
return 60;