Remove and add Subview to Custom UITableViewCell - ios

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];
}
}
}

Related

Checkmark in tableViewCell hidden when Scroll

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]];
}
}
}

Control may reach end non-void function [duplicate]

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.

How to store, retrieve check or unchecked cells

I am trying to save the checkmark that i place on a cell and then retrieve it. I have 11 cells in that section.
I tried to use this link as a guide and it works. But i want only need one checkmark to be saved rather than many.
And also i want to set checkmark for all cells by default. How would i achieve this
My Code :
- (NSString *)getKeyForIndex:(int)index{
return [NSString stringWithFormat:#"KEY%d",index];
}
- (BOOL) getCheckedForIndex:(int)index {
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES) {
return YES;
}
else
{
return NO;
}
}
- (void) checkedCellAtIndex:(int)index
{
BOOL boolChecked = [self getCheckedForIndex:index];
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(section == 4) {
if([self getCheckedForIndex:indexPath.row]==YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
NSString *key = [_keys objectAtIndex:indexPath.section];
NSArray *name = [_names objectForKey:key];
static NSString *MyCellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [name objectAtIndex:indexPath.row];
if(section == 4) {
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Solution can be tricky :
Maintain one array same to tableview datasource array.
Say for example u have NSMutableArray *mutArrNames which tableview datasource. Now NSMutableArray *mutArrNamesCheckListwhich shows which one is checked or not;
Intially nothing is selected so add 0 to uncheck one:
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"0"];
}
Now use like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(section == 4) {
//check from mutArrNamesCheckList's list if cell is check one or not
if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else //unchecked one
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
Here it can be used according to your requirement like allow mutiple selection or single selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get cell object
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(section == 4)
{
if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//replace 0 with 1 as check one
[mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:#"0"];
}
else //unchecked one
{
cell.accessoryType = UITableViewCellAccessoryNone;
//replace 1 with 0 as uncheck one
[mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:#"1"];
}
}
}
For selection and unselection of all cell add this in button's action method. Use this :
//remove all as new objects will be added
[mutArrNamesCheckList removeAllObjects];
if(!btnCheckAll.selected)//selected property used - default is no so all not selected
{
//make all selected
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"1"];
}
//change buttons image here
}
else
{
//make all unselected
for(int i =0; i<[mutArrNames count];i++)
{
[mutArrNamesCheckList addObject:#"0"];
}
//change buttons image here
}
//tableView reload
[yourtableView reloadData];
btnCheckAll.selected = !btnCheckAll.selected;
Add mutArrNamesCheckList in AppDelegate to be visible in whole Application.
So remove iteration we done on top of above answer in our viewcontroller as we didn't wanted to use gobally
EDIT : added toggling in didSelectRowAtIndexPath + clicked select all button, all cell is checked else unchecked

UITableView + 2 Custom Cells = Wrong with heights

I have a UITableView which should show news.
I did set up 2 custom cells -> one has the news without an image, and the other one has the news with image.
So the cells need different heights. In storyboard I created those 2 Cells and they each have a custom height (280 with image, 130 without).
For testing purposes I want the first and the third cell to be a news cell + image.
So thats my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary * news = [postArray objectAtIndex:indexPath.row]; //PostArray is the array with the news
NSDictionary *authorData = [news valueForKey:#"author"];
NSString *postTitle = [self decodedString:[news valueForKey:#"title"]];
//Decode = change html chars
NSString *postExcerpt = [self decodedString:[news valueForKey:#"excerpt"]];
NSString *postComments = [news valueForKey:#"comment_count"];
//NSString *postID = [news valueForKey:#"id"];
NSString *authorFirstName = [self decodedString:[authorData valueForKey:#"first_name"]];
NSString *authorLastName = [self decodedString:[authorData valueForKey:#"last_name"]];
NSString *authorNickName = [self decodedString:[authorData valueForKey:#"nickname"]];
if (indexPath.row == 0 || indexPath.row == 2){
NewsCellImage *cell = (NewsCellImage *)[tableView dequeueReusableCellWithIdentifier:#"NewsCellImage"];
if (cell == nil) {
cell = [[NewsCellImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"NewsCellImage"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:#"%i",indexPath.row];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:#"%# / %#",authorNickName,[news valueForKey:#"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:#"%# %# / %#",authorFirstName,authorLastName,[news valueForKey:#"date"]];
}
//Set Image
NSDictionary *imageData = [news valueForKey:#"thumbnail_images"];
NSDictionary *imageDataFull = [imageData valueForKey:#"large"];
NSString *postImage = [imageDataFull valueForKey:#"url"];
[cell.postImage setImageWithURL:[NSURL URLWithString:postImage]
placeholderImage:[UIImage imageNamed:#"menu"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (error){
NSLog(#"Error bei Bild laden: %#",postTitle);
}
} usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
return cell;
} else {
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:#"NewsCell"];
if (cell == nil) {
cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"NewsCell"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:#"%#",postComments];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:#"%# / %#",authorNickName,[news valueForKey:#"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:#"%# %# / %#",authorFirstName,authorLastName,[news valueForKey:#"date"]];
}
return cell;
}
}
The code does work in general and thats what I can see after starting the app:
http://cl.ly/image/320K1y081T3Z
But when I scroll down to the next cell with Image something goes wrong:
http://cl.ly/image/2M2O1X3R2T13
So something is wrong with my code or I need to add something - but I dont know which way to take.
Maybe it's something with
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
But I really dont know. I appreciate help. Ty
Try this method.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexPath.row == 0 || indexPath.row == 2){
return newsWithImageHeight;
}
else return newsHeight;
}
Or you can check autolayout setting for cell in interfacebuilder
How does your:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
look then?
I think you need something like:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexpath.row == 0 || indexpath.row == 2) {
return 280;
}
return 130;
}

How can I solve this delegate error?

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.

Resources