I tried this but getting an exception error at cellforrowatindexpath
Below is the exception I got.
Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84
if(aTableView==specTable)
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [specTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier];
}
return cell;
}
else
{
static NSString *CellIdentifier2 = #"cell2";
UITableViewCell *cell= [table2 dequeueReusableCellWithIdentifier:ReviewCellIdentifier2];
}
return cell;
Two problems:
You never return cell2. At the end of this method, you always return cell, regardless of whether the sender table view was equal to the first or second one.
You don't create the cell as you do in the first part if in the second part (the else branch) the dequeueReusableCellWithIdentifier: message returns nil.
All in all:
if (aTableView == specTable)
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [specTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease]; // you were also leaking memory here
}
return cell;
}
else
{
static NSString *cellIdentifier2 = #"cell2";
UITableViewCell *cell2 = [table2 dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell2 == nil)
{
cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
}
return cell2;
}
return nil; // just to make the compiler happy
Related
I have the following implementation, where I am adding a footerview as a customcell. However, if there is no content, I do not want to show the last cell, if there is I want to show it.
In my current implementation, it always displays the last cell.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.adminOrderElements count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return adminOrderElements[section].products.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
There is one option that you can make cellHeight to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(CHECK_IF_NO_CONTAIN)
return 0;
return 40;// Normal height as you want
}
I think you can add condition in else clause like if certain condition is met then you want to display this cell other wise not...
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
Its quite simple with UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (dataObjects.count == 0){
return 0;
}
return dataObjects.count;
}
I have a UITableView which is populated with an NSArray. The array is sorted alphabetically in my table with alphabetized headers.
I am adding a custom cell randomly in my table at every 10th row.
The issue I have is that the custom cell is layering over actually data, so it's not being inserting within the array.
How can I solve this?
Here's what I am doing to insert the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 10 == 10-1) {
CustomAdCell *cell = (CustomAdCell *)[tableView dequeueReusableCellWithIdentifier:#"AdCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomAdCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
else {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *key = [[[self indexedMembers] allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)][indexPath.section];
self.indexedMembersArray = ((NSMutableArray *)[self indexedMembers][key]);
MemberInfo *currentMember = self.indexedMembersArray[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currentMember.firstName, currentMember.lastName];
return cell;
}
I have two table view in one view controller.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag==0) {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell=[[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.title.text=[[stories objectAtIndex: indexPath.row] objectForKey: #"title"];
cell.title.numberOfLines=6;
return cell;}
else
{
static NSString *CellIdentifier2 = #"Cell2";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
int row =indexPath.row;
if (row==0) {
cell.title.text =#"All Songs";
}
else
cell.title.text = [categoryArray objectAtIndex:indexPath.row-1];
return cell;}
}
For the first (tableView.tag==0) table everything it's ok , but for the second :
2015-02-18 11:53:11.733 Karaoke Final Project[2722:808305] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell title]: unrecognized selector sent to instance 0x7fb4d2c48bb0'
For both table view cell i set the same class TableViewCell.
If tableView.tag != 0 i put
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
I don't have problems.
cell=[[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
the return type of cell type is UITableViewCellStyleSubtitle so there is no property title is this style.
You have to use either
cell.textLabel.text = #"Title";
or
cell.detailTextLabel.text = #"Details";
All cell objects are UITableViewCell type, because you alloc them as such ones. And as far as I know UITableViewCell does not have property named "title".
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
It does not make your object TableViewCell type. It just creates pointer of this type. It works, because you inherit from UITableViewCell in TableViewCell.
You should try this:
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
instead of:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
in both cases.
static NSString *CellIdentifier2 = #"Cell2";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
use the code above.... for more details visit this link..
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
i'm trying to use two UITableView in one ViewController. one UITableView with the reference of custom cell and the other one is simple...I've written this code but it gives me error of control may reach end of non-void function...
so give me the suggestion for it...what can i do...?
thanks in advance...
here, is my code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
return cell1;
}
else if (tableView==self.listTable)
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
return cell2;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
cell=cell1;
}
else if (tableView==self.listTable)
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
cell=cell2;
}
return cell;
}
You can lose the second if condition
if (tableView==self.categoryTable)
{
//do stuff
return cell1;
}
else
{
//do stuff
return cell2;
}
try this, you have to init the type the cell as UITableviewViewCell and cast after. Tell me if this sample answer to your question.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView==self.categoryTable) {
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[category objectAtIndex:indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor redColor];
} else if (tableView==self.listTable) {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
cell = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = (RWTListCell *)[nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
((RWTListCell *) cell).textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (tableView==self.categoryTable) {
...
...
return cell1;
}
else if (tableView==self.listTable) {
...
...
return cell2;
}
retrun cell;
}
Try this, take a dummy cell and return it at the end.
Actually your problem is not returning any cell to tableview.
I have altered your code. I just removed your else if condition
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.categoryTable)
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell1.textLabel.text=[category objectAtIndex:indexPath.row];
cell1.textLabel.highlightedTextColor = [UIColor redColor];
return cell1;
}
static NSString *simpleTableIdentifier = #"SimpleTableCell";
RWTListCell *cell2 = (RWTListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
//NSLog(#"%ld",(long)indexxxx);
//NSLog(#"%lu",(unsigned long)imagesmarry.count );
cell2.textLabel.text=[[imagesmarry_pictitle objectAtIndex:indexxxx]objectAtIndex:indexPath.row];
//NSLog(#"%#",[imagesmarry_pictitle objectAtIndex:indexxxx]);
return cell2;
}
Just add return; at the last.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (condition 1) {
// statements
return cellType_1;
} else if (condition 2) {
//statements
return cellType_2;
}
return;
}
or you can also do this -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (condition 1) {
// statements
return cellType_1;
} else {
//statements
return cellType_2;
}
}
remove the second condition . because you must return in non void method weather your all condition is true or not.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LibraryListingCell";
InSeasonCell *cell = (InSeasonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"InSeasonCellView" owner:self options:nil];
cell = [_cell autorelease];
_cell = nil;
}
if(_dataController!=NULL){
Product *productAtIndex = [_dataController objectInListAtIndex:indexPath.row];
// Configure the cell...
if (productAtIndex.name != nil && productAtIndex.week != nil && productAtIndex.image != nil) {
cell.name.text = productAtIndex.name;
cell.week.text = productAtIndex.week;
cell.image.image = productAtIndex.image;
}
}
return cell;
}
Message ERROR for cell.name.text cell.week.text cell.image.text. Pretty sure it is a memory management error. I have retained and released properly to the best of my knowledge. The application will crash upon launch, sometimes it loads everything fine, but when you scroll it crashes. Any help or pointers about memory management is appreciated.
Instead of this:
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"InSeasonCellView" owner:self options:nil];
cell = [_cell autorelease];
_cell = nil;
}
You sent autorelease message and set it to nil, later you are trying to access that released cell.
I think it should be as:
static NSString *CellIdentifier = #"LibraryListingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}