This question already has answers here:
what if when two UITableView in one ViewController and one with custom cell refrence and another is a simple
(6 answers)
Closed 8 years ago.
The attached code is returning an error:
Control may reach end non-void function
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:#"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil) {
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1) {
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:#"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil) {
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SecondCustomCell"];
}
return sCustomCell;
}
} //<-- Control may reach non-void function (I precise that's the end of the cellForRowAtIndexPath method)
I know that the problem is specially at the "return" but how do I eliminate the error?
You account for the cases where indexPath.row == 0 and where indexPath.row == 1 but the compiler is saying: What should I return if the row isn't 0 or 1?
You probably want a return nil; at the end of your method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:#"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil)
{
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1)
{
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:#"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil)
{
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"secondCustomCell"];
}
return sCustomCell;
}
return nil; //<--Add this line
}
Or perhaps an "else" case:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:#"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil)
{
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1)
{
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:#"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil)
{
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"secondCustomCell"];
}
return sCustomCell;
}
else //<--Add this clause
{
OtherCustomCell *oCustomCell = [tableView dequeueReusableCellWithIdentifier:#"otherCustomCell" forIndexPath:indexPath];
if (oCustomCell == nil)
{
oCustomCell = [[OtherCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"otherCustomCell"];
}
return sCustomCell;
}
}
Note: You also have a typo in your reuse identifiers:
"secondCustomCell" is not the same as "SecondCustomCell"
Add a return nil; line.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:#"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil) {
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1) {
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:#"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil) {
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SecondCustomCell"];
}
return sCustomCell;
}
return nil;
}
I might add that you need to make sure that you fCustomCell and your sCustomCell are not (!=) nil.
if (!fCustomCell) {
fCustomCell = [UITableViewCell alloc] initWithStyle:/*aStyle*/ reuseIdentifier:/*identifier*/];
}
The identifier can be a static NSString defined at the beginning of the method like so: static NSString *identifier = #"cell";
Look at some tutorials.
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;
}
When i scroll Table, checkmark is hide. I know because of Reusing Cell, but I dont know how to fix. Pls help me. Here is my code:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
I change check and uncheck in didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[dict setObject:courseStudent.id_user forKey:#"student_id"];
[studentDetail addObject:dict];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}
When you scroll table view, cellForRowAtIndexPath for particular cell will be called in which your are setting the accessoryType as None. Instead try like below.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
if ([studentDetail containsObject:courseStudent]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[dict setObject:courseStudent.id_user forKey:#"student_id"];
[studentDetail addObject:dict];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}
Hope this will help.
If you are reusing cells then you need to save the state of each cell .Because every time you scroll up and down, TableView will bring back the previous cell that outside of the screen.
You can save selectable state in an array and read its index in cellForRowAtIndexpath to get the current state of the cell.
You can also do this by adding one boolean property like is-selected in your "TeacherInfo" NSObject class,and set true false based on table-row selection.
Try this :
You need to add instance object of TeacherInfo instead of student id because your containedObject of array gives wrong result .
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
StudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[StudentTableViewCell alloc] init];
}
if (_btnCancel.hidden == NO) {
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
if ([studentDetail containsObject:courseStudent]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if (_btnCancel.hidden == NO) {
if (cellCheck.accessoryType == UITableViewCellAccessoryNone) {
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
TeacherInfo *courseStudent = studentQuitArray[indexPath.row];
[studentDetail addObject:courseStudent];
} else {
cellCheck.accessoryType = UITableViewCellAccessoryNone;
[studentDetail removeObject: studentQuitArray[indexPath.row]];
}
}
}
I have issue, my app crash on cellForRowAtIdexPath because I want to add 2 different custom table view cells for 2 different rows.
See my code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MatchDetail";
MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
static NSString *CellIdentifier2 = #"GoalsList";
GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
if (cell1 == nil) {
cell1 = (MatchDetailsCell *)[MatchDetailsCell cellFromNibNamed:#"MatchDetailsCell"];
}
[cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
return cell1;
}
else if (indexPath.row == 1)
{
if (cell2 == nil) {
cell2 = (GoalsListCell *) [GoalsListCell cellFromNibNamed:#"GoalsListCell"];
}
[cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
return cell2;
}
}
return nil;
}
Your code is incorrect. You must dequeue the cell of this row with this identifier.
If you have only one section in the table, you don't need to do the indexPath.section check. Try this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"MatchDetail";
static NSString *CellIdentifier2 = #"GoalsList";
if(indexPath.row == 0) {
//The first row is the MatchDetailsCell
MatchDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell == nil) {
//If cell not exists, you must create a new one
cell = [[MatchDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
//Rest of your cell code here
return cell;
} else {
//Rest of the cells are GoalsListCell
GoalsListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell == nil) {
cell = [[GoalsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
//Rest of your cell code here
return cell;
}
}
I don't have more information about your code, but maybe it helps you.
Try that, you shouldn't deque the cell first and after that you load it from nib.
If it doesn't work post crash log.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MatchDetail";
static NSString *CellIdentifier2 = #"GoalsList";
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
MatchDetailsCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
[cell1 setDataInCell:arrAllGames :strTeamA :strTeamB];
return cell1;
}
else if (indexPath.row == 1)
{
GoalsListCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
[cell2 setDataInCell:arrGoalsList :[arrAllGames count]];
return cell2;
}
}
return nil;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row%2)==0) // for even rows, like 1,3,5,...
{
MyFirstCell *firstCell = (MyFirstCell *)[tableView dequeueReusableCellWithIdentifier:#"MyFirstCell"];
if (firstCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyFirstCell" owner:self options:nil];
firstCell = (MyFirstCell *)[nib objectAtIndex:0];
}
return firstCell;
}
else // for even rows, like 2,4,6,...
{
MySecondCell *secondCell = (MySecondCell *)[tableView dequeueReusableCellWithIdentifier:#"MySecondCell"];
if (secondCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MySecondCell" owner:self options:nil];
secondCell = (MySecondCell *)[nib objectAtIndex:0];
}
return secondCell;
}
return nil;
}
i have a Custom Cell with different Subviews on it. One of them, a UIImageView, is optional. so there can be the case that no Image is needed in the cell. My Problem is that i dont know how i can remove the Image for cells which dont need it, and how to add the image for cell which need it? I know i only should add and remove subviews like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = #"NewsCell";
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(newsCell == nil){
newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
//Here i get the Image from my Array (self.newsList)
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:#"boardnoteImage"];
//Here im checkin if an image exists
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
//If no Image exists remove the subview
[newsCell.boardNoteImage removeFromSuperview];
} else {
//If an image exists, check if the subview is already added
if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
[newsCell.contentView addSubview:[newsCell.boardNoteImage];
}
}
}
But that doesnt work because now i get the same Image on each cell......
EDIT:
Sry i forget to say that i´m using Section with only one row per section, thats the reason why i´m using indexPath.section!
I get all my Datas, from an webserver and put all the stuff into my array self.newsList!
EDIT2:
With this Code i get the correct Images for the correct Cells, but after scrolling down and back up, everyhting is disappeared :/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = #"NewsCell";
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(newsCell == nil){
newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:#"boardnoteImage"];
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
[newsCell.boardNoteImage removeFromSuperview];
newsCell.boardNoteImage.image = nil;
} else {
if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
newsCell.boardNoteImage.frame = CGRectMake(newsCell.boardNoteImage.frame.origin.x, newsCell.boardNoteImage.frame.origin.y, newsCell.boardNoteImage.frame.size.width, newsCell.boardNoteImage.frame.size.height);
[newsCell.contentView addSubview:newsCell.boardNoteImage];
}
}
Try this approach - Add UIImageView in storyboard and you can just hide and unhide whenever you app logic required.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SongCell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SongCell"];
//dont check the presence of image in this part becz it is called during creation only not on resuing
}
//check if image is present or not
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:#"boardnoteImage"];
if(boardImg)
{
//image is present
cell.boardNoteImage.hidden = NO;
cell.boardNoteImage.image = boardImg;
}
else
{
cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
cell.boardNoteImage.hidden = YES;
}
return cell;
}
Please use this one:
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:#"boardnoteImage"];
You require to use indexPath.row instead of indexPath.section
You are adding the image for section. So, do as following :
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:#"boardnoteImage"];
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
//If no Image exists remove the subview
[newsCell.boardNoteImage removeFromSuperview];
newsCell.boardNoteImage = nil;
} else {
//set the frames here if required
[newsCell addSubview:boardNoteImage];
}
Edit 1 :
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
[newsCell.boardNoteImage removeFromSuperview];
newsCell.boardNoteImage.image = nil;
} else {
if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
newsCell.boardNoteImage = [[UIImageView alloc] init];
// SET THE IMAGE HERE
newsCell.boardNoteImage.frame = CGRectMake(newsCell.boardNoteImage.frame.origin.x, newsCell.boardNoteImage.frame.origin.y, newsCell.boardNoteImage.frame.size.width, newsCell.boardNoteImage.frame.size.height);
[newsCell.contentView addSubview:newsCell.boardNoteImage];
}
try by this way, dont check the presence of image inside if condition do like below hope this helps u, change it to your requirement
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SongCell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SongCell"];
//dont check the presence of image in this part becz it is called during creation only not on resuing
}
//check if image is present or not
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:#"boardnoteImage"];
if(boardImg)
{
//image is present
cell.boardNoteImage.image = boardImg;
}
else
{
cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
cell.boardNoteImage. hidden = YES;//place a nil in the image
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//static NSString *CellIdentifier1 = #"NewsCell";
NSString *CellIdentifier1 = [NSString stringWithFormat:#"cell %d",indexPath.row];
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
newsCell = nil;
if(newsCell == nil)
{
newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
//Here i get the Image from my Array (self.newsList)
NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:#"boardnoteImage"];
//Here im checkin if an image exists
if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
//If no Image exists remove the subview
[newsCell.boardNoteImage removeFromSuperview];
} else {
//If an image exists, check if the subview is already added
if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
[newsCell.contentView addSubview:[newsCell.boardNoteImage];
}
}
}
I have a delegate, but it doesn't work without the other delegate line in it:
- (id)initWithData:(NSData *)data delegate:(id <ParseOperationDelegate>)theDelegate {
self = [super init];
if (self != nil) {
self.dataToParse = data;
self.delegate = theDelegate;
}
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
It should load data in a Cell but the data isn't shown before the first scroll. What can I do?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"productCell";
static NSString *PlaceholderCellIdentifier = #"placeholderCell";
int nodeCount = [appDelegate.products count];
if (nodeCount == 0 && indexPath.row == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlaceholderCellIdentifier];
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.detailTextLabel.text = #"Loading...";
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (nodeCount > 0) {
XMLProductView *listedProduct = [appDelegate.products objectAtIndex:indexPath.row];
cell.textLabel.text = listedProduct.name;
// cell.detailTextLabel.text = appRecord.artist;
if (!listedProduct.productImage) {
if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
[self startImageDownload:listedProduct forIndexPath:indexPath];
}
UIImageView *listedImage = (UIImageView *)[cell viewWithTag:1];
listedImage.image = [UIImage imageNamed:#"Placeholder.png"];
}
else {
UIImageView *listedImage = (UIImageView *)[cell viewWithTag:1];
listedImage.image = listedProduct.productImage;
}
}
return cell;
}
your code seems that its waiting for some event, or some files to get fetched or downloaded, so you will need to call [tableview reloadData]; when this data is downloaded,
I cant figure it out from the code, but my guts tells me this
1st:
that you are waiting for data from int nodeCount = [appDelegate.products count]; to be ready, so you will need to have some sort of delegate that gets called when this data is ready
2nd:
you are waiting for an image to get downloaded here [self startImageDownload:listedProduct forIndexPath:indexPath], so when this actually get downloaded you will need to set the image to that cell or reloadData on the table
That is what i can figure out from the code.