UITableView + 2 Custom Cells = Wrong with heights - ios

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

Related

Obj-c - If no search results, return 1 cell in tableview?

When my user searches for something inside a tableview, when no results are returned, my app shows the standard "No Results" placeholder inside the tableview. That said, when no results exist, I want to return one populated cell (cell populated with default data). How can I accomplish this? I tried the below, but I still get 'No Results' returned?
ViewController.m
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchResults count] == 0) {
return 1;
} else {
return [searchResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NetworkTableIdentifier = #"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"sidebarCell" owner:self options:nil];
cell = nib[0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSDictionary *userName = searchResults[indexPath.row];
NSString *first = userName[#"first name"];
NSString *last = userName[#"last name"];
[[cell username] setText:[NSString stringWithFormat:#"%# %#", first, last]];
NSDictionary *userlast = searchResults[indexPath.row];
[[cell lastName] setText:userlast[#"last name"]];
NSDictionary *userBio = searchResults[indexPath.row];
[[cell userDescription] setText:userBio[#"userbio"]];
NSString *area = userName[#"neighbourhood"];
NSString *city = userName[#"city"];
[[cell areaLabel] setText:[NSString stringWithFormat:#"%#, %#", area, city]];
NSString *profilePath = searchResults[indexPath.row][#"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
if ([searchResults count] == 0) {
NSLog(#"SEARCH RESULTS ARE %#", searchResults);
[[cell username] setText:[NSString stringWithFormat:#"%#", self.searchBar.text]];
[[cell lastName] setText:userlast[#""]];
[[cell userDescription] setText:#"This friend is not on the app (yet!) Tap to invite them."];
[[cell areaLabel] setText:[NSString stringWithFormat:#""]];
NSString *profileDefault = #"http://url.com/user.png";
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profileDefault]];
return cell;
}
return cell;
}
I don't really recommend doing this, as you should return an empty list, if there are no search results. That is consistent with User Interface Guidelines. But, if you insist, you could create a default object and initialize your searchResults array with that object and return 1 from the numberOfRows method. Something like this:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
if ([searchResults count] == 0) {
NSDictionary *dict = [NSDictionary dictionaryWithObjects:#[#“Enter First Name”, #“Enter Last Name”, #“Enter User Bio”, #“Enter Neighborhood”, #“Enter City”, #“Enter Photo Path”]
forKeys: #[#“first_name”, #“last_name, #“userbio”, #“neighbourhood”, #“city”, #“photo_path”];
searchResults = [NSArray arrayWithObjects: dict, nil];
return 1;
}
else {
return [searchResults count];
}
}
And, you can greatly simplify your cellForRowAtIndexPath code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NetworkTableIdentifier = #"sidebarCell";
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
//Note, I like to name my local variables the same as the dictionary keys just to eliminate any confusion
NSDictionary *userObject = [searchResults objectAtIndex:indexPath.row];
NSString *first_name = [userObject objectForKey:#"first name"];
NSString *last_name = [userObject objectForKey:#"last name"];
NSString *userbio = [userObject objectForKey:#“userbio”];
NSString *neighbourhood = [userObject objectForKey:#“neighbourhood”];
NSString *city = [userObject objectForKey:#“city”];
NSString *photo_path = [userObject objectForKey:#“photo_path”];
[[cell username] setText:[NSString stringWithFormat:#"%# %#", first_name, last_name]];
[[cell lastName] setText:last_name];
[[cell userDescription] setText:userbio];
[[cell areaLabel] setText:[NSString stringWithFormat:#"%#, %#", neighbourhood, city]];
[[cell usermini] sd_setImageWithURL:[NSURL URLWithString:photo_path]];
}
return cell;
}
I did something like this in my app. It's ugly and I'm not recommending you following this way. I did it only because I was kind of lazy about layouts and placing the placeholder in the correct place in the view hierarchy and handle all those hide/show situations. My view controller has a very complex hierarchy of views and the table view was one that has already had all I needed (resize automatically when the status or toolbar is showing).
What I suggest you is to hide the table view when there is an empty search result and substitute it with your placeholder.
Try this it's working:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (arrData.count>0)
{
self.TblView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.TblView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.TblView.bounds.size.width, self.TblView.bounds.size.height)];
noDataLabel.text = #"No Results";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
self.TblView.backgroundView = noDataLabel;
self.TblView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}

UITableView pops up no data before it has the data

I am using hpple to get data from web page and display it in table view, but i want to cells to display " no data" if there either is no data or it can't get the data. But because of this it flases the no data on the view controller for about a second when the data is loaded here is the code i am using:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
if ((_deli.count > 0)) {
return _deli.count;
}
else {
return 1;
}
break;
case 1:
if ((_entrees.count > 0)) {
return _entrees.count;
}
else {
return 1;
}
break;
}
return 0
}
and then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:17.0f];
if (indexPath.section == 0) {
if ([tableView numberOfRowsInSection:0] == _deli.count ) {
Items *thisDeli = [_deli objectAtIndex:indexPath.row];
cell.textLabel.text = thisDeli.title;
}
else { cell.textLabel.text = #"No Data Avaiable";
}
}
else if (indexPath.section == 1) {
if ([tableView numberOfRowsInSection:1] == _entrees.count) {
Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEntree.title;
}
else { cell.textLabel.text = #"No Data Avaiable";
}
}
}
Is there a way to delay the appearance of the "no data" phrase or only display it if it is blank or can't get it.
Heres my NSURLConnection
- (void) loadDataUsingNSURLConnection {
NSString *url = #"http://ueat.site88.net/westTest.xml";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(#"response == %#", response);
}
Thanks for your help in advance.
Cheers
Consider making the "No Data Available" string dynamic based on whether your call has completed to load the data or not. If the call has not completed have it use "Loading" or something like that. If the call has completed have it show the existing "No Data Available" string.
EDIT WITH EXAMPLE:
The easiest thing to do, with the code you have, is to set your _deli and _entrees variables to nil until you've loaded them, and check for that condition when displaying your message:
#interface YourViewController ()
{
NSArray *_deli;
NSArray *_entrees;
}
#end
Set the arrays to nil in your viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
_deli = nil;
_entrees = nil;
}
Change the logic in your previous code to this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
if (_deli == nil || _deli.count == 0) {
return 1;
}
else {
return _deli.count;
}
break;
case 1:
if (_entrees == nil || _entrees.count == 0) {
return 1;
}
else {
return _entrees.count;
}
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:17.0f];
if (indexPath.section == 0) {
if (_deli == nil)
cell.textLabel.text = #"Loading";
else if (_deli.count > 0) {
Items *thisDeli = [_deli objectAtIndex:indexPath.row];
cell.textLabel.text = thisDeli.title;
}
else
cell.textLabel.text = #"No Data Avaiable";
}
else if (indexPath.section == 1) {
if (_entrees == nil)
cell.textLabel.text = #"Loading";
else if (_entrees.count > 0) {
Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEntree.title;
}
else
cell.textLabel.text = #"No Data Avaiable";
}
}
Since your loadDataUsingNSURLConnection just writes out the response I'm not sure how you're loading the array, but you should get the idea at this point. All you need to do is just load the response into the array and call the reloadData method on your table to get the new data to appear.

Tableview cells only displaying correctly on scroll up

I'm struggling with creating a tableview with custom, subclassed cells created in storyboards.
All the cells work well except the "questionCell". This cell only shows a fragment of its textfield (or sometimes none of it) until I scroll down and then back up again.
The initial appearance:
After scrolling down then up again:
Where am I going wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"TextCell";
static NSString *CellIdentifier2 = #"QuestionCell";
static NSString *CellIdentifier3 = #"ImageCell";
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
UITableViewCell *cell = nil;
if (section == 0){
//the intro section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = _intro;
cell = textCell;
} else if (section == 4){
//this is the "the answer is . . ." section
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
textCell.labelText.text = [NSString stringWithFormat:#"The only EOM to not be eliminated is %#",_theAnswer];
cell = textCell;
} else {
if (row == 0) {
NSString *item = [NSString stringWithFormat:#"question_%li", (long) section];
NSString *txt = [self valueForKey: item];
QuestionCell *qCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
qCell.questionText.text = txt;
cell = qCell;
} else if (row == 1) {
NSString *item = [NSString stringWithFormat:#"img_%li", (long)section];
NSString *img = [self valueForKey: item];
ImageCell *iCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];
iCell.image.image = [UIImage imageNamed: img];
cell = iCell;
} else {
TextCell *textCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
NSString *item = [NSString stringWithFormat:#"explain_%li", (long)section];
NSString *txt = [self valueForKey: item];
textCell.labelText.text = txt;
cell = textCell;
}
}
return cell;
}
Here's heightForRowAtIndexPath. The results are the same if I just comment this out and set the heights in the prototype cells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Return NO if you do not want the specified item to be editable.
CGFloat hgt = 70;
if (indexPath.section == 0){
//the intro
hgt = 150;
} else if (indexPath.section == 4){
//the "answer is . . ."
hgt = 100;
} else {
if (indexPath.row == 1) {
//the image
hgt = 225;
} else if (indexPath.row == 2){
//the discussion
hgt = 180;
}
}
return hgt;
}
the questionCell just sets the iboutlet:
#import <UIKit/UIKit.h>
#interface QuestionCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *questionText;
#end

Remove and add Subview to Custom UITableViewCell

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

how to change the UITableViewCell

I have 2 tableViewCell in tabbar. copy values ​​from one button to another, and if the values ​​match the counter is incremented, and the Cell is removed. In managedCoreData
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * result = nil;
static NSString * OrederTableViewCell = #"OrderTableViewCell";
result = [tableView dequeueReusableCellWithIdentifier:OrederTableViewCell];
if (result == nil)
{
result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OrederTableViewCell];
result.selectionStyle = UITableViewCellSelectionStyleNone;
}
Order * order = [self.orderFRC objectAtIndexPath:indexPath];
result.textLabel.text = order.name;
result.imageView.image = [UIImage imageWithData:[order imageSmall]];
self.count = 1;
if ([order.name characterAtIndex:indexPath.row] == [order.name characterAtIndex:indexPath.row+1])
{
self.count++;
[[self managedObjectContext]deleteObject:order];
}
result.detailTextLabel.text = [NSString stringWithFormat:#"X = %d",self.count];
return result;
}

Resources