How to remove duplicated items in iCarousel - iphone

https://github.com/nicklockwood/iCarousel/issues/278
I am going to use the iCarousel to show the images linearly.
These images will be gotten dynamically.
Assume that there are 2 images.
But when I update iCarousel, there are also 2 images in background as following image.
While debugging code, I found that viewForItemAtIndex function is called 2 times after I called reloadData function.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
So there are 4 images in iCarousel.
How to prevent to be called 2 times?
Please help me to fix this problem asap.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
if (view == nil)
{
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.text = userFriend.m_strName;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
[view addSubview:puzzlePhoto];
[view addSubview:puzzleBG];
[view addSubview:label];
}
return view;
}

The problem is almost certainly due to the views being recycled incorrectly. Views are re-used with different indexes, you you should never set index-specific properties in your view creation code. Here is the correct way to set up your item view:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
const NSInteger puzzlePhotoTag = 1;
const NSInteger puzzleBGTag = 2;
const NSInteger labelTag = 3;
if (view == nil)
{
//*************************************************
//do setup that is the same for every item view here
//*************************************************
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
puzzlePhoto.tag = puzzlePhotoTag;
[view addSubview:puzzlePhoto];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
puzzleBG.tag = puzzleBGTag;
[view addSubview:puzzleBG];
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
label.tag = labelTag;
[view addSubview:label];
}
else
{
//get references to subviews
puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
label = (UILabel *)[view viewWithTag:labelTag];
}
//*************************************************
//do setup that is different depending on index here
//*************************************************
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label.text = userFriend.m_strName;
return view;
}
As for the scrolling inertia problem, if you're still seeing that after applying this fix, file a separate bug report on the github page.

I solved this problem by performing reloadData on mainThread
[carousel performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];

Related

iCarousel repeating Images

I am using amazing iCarousel library in my app. But i am facing a problem. I am using this code to populate data into carousel.
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
return [items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
UIImageView *imageView = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 60, [[UIScreen mainScreen] bounds].size.height -150)];
view.contentMode = UIViewContentModeCenter;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGRect webFrame = CGRectMake(0.0, 0.0, width - 60, width - 60);
imageView = [[ UIImageView alloc ] initWithFrame:webFrame];
[view addSubview:imageView];
label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 240.0, width -60, 260.0)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:22];
label.numberOfLines = 5;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.tag = 1;
[view addSubview:label];
}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
NSString *imageName = [NSString stringWithFormat:#"%#.pdf",[[items objectAtIndex:index] objectAtIndex:0]];
NSLog(#"Image File Name: %#", imageName);
NSLog(#"Image Explanation: %#", [[items objectAtIndex:index] objectAtIndex:1]);
[imageView setImage:[ UIImage imageWithPDFNamed:imageName atSize:CGSizeMake( 150, 130 ) ]];
label.text = [[items objectAtIndex:index] objectAtIndex:1];
return view;
}
numberOfItemsInCarousel returns 38 but carousel only shows 6 images and then repeating them in cycle. Lable text is showing according to array index correctly.
How can i Fix it?
You are not getting imageView from reused views, read the line
[imageView setImage:[ UIImage imageWithPDFNamed:imageName atSize:CGSizeMake( 150, 130 ) ]];
as
[nil setImage:[ UIImage imageWithPDFNamed:imageName atSize:CGSizeMake( 150, 130 ) ]];
when view != nil. Get the imageView in the same way you do it for label.

Add another view in GMGridView

Hello all,
I have to add one more view in cell of GMGridView. But i am unable to do this because i have to drag my label from view to view1.
My code is :
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView1 cellForItemAtIndex:(NSInteger)index
{
// set size based on orientation
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
if (!cell)
{
cell = [[[GMGridViewCell alloc]init]autorelease];
//one view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.backgroundColor = [UIColor redColor];
view.layer.masksToBounds = NO;
view.layer.cornerRadius = 2;
cell.contentView = view;
//another view
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 80, size.width, size.height)];
view1.backgroundColor = [UIColor yellowColor];
view1.layer.masksToBounds = NO;
view1.layer.cornerRadius = 2;
cell.contentView = view1;
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
// allocate label
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.text = (NSString *)[self.currentData objectAtIndex:index];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:20];
[cell.contentView addSubview:label];
return cell;
}
height of cell is 200. but still it shows only one view.
Loading Two types of Cell in GMGridView
This is your Solution, it works as charm..
First Do this Exactly
-(void)prepareExhibitor
{
NSInteger spacing = 1.0;
CGRect rect=self.view.frame;
rect.origin=CGPointMake(0, 0);
self.gmGridView = [[GMGridView alloc] initWithFrame:rect];
self.gmGridView.backgroundColor = [UIColor clearColor];
self.gmGridView.centerGrid=NO;
self.gmGridView.style = GMGridViewStylePush;
self.gmGridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
self.gmGridView.showsHorizontalScrollIndicator=FALSE;
self.gmGridView.clipsToBounds=YES;
self.gmGridView.itemSpacing = spacing;
self.gmGridView.minEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);
[self.viewNewsHeadline addSubview:self.gmGridView];
self.gmGridView.actionDelegate = self;
self.gmGridView.dataSource = self;
self.gmGridView.mainSuperView = self.superView;
}
Then Write this accordingly as per your Code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
GMGridViewCell *cell = [gridView dequeueReusableCell];
NewsCell_iPad *view;
UIViewController *controller;
if (!cell)
{
// [self removeGrid];
cell = [[GMGridViewCell alloc] init];
if(index%2==0)
{
controller=[[UIViewController alloc] initWithNibName:#"NewsCellTypeA_iPad" bundle:nil];
}
else
{
controller=[[UIViewController alloc] initWithNibName:#"NewsCellTypeB_iPad" bundle:nil];
}
if(!view)
{
view=(NewsCell_iPad *)controller.view;
}
cell.layer.masksToBounds = NO;
cell.contentView = view;
}
NewsCell_iPad *newsView=(NewsCell_iPad *)cell.contentView;
newsView.news=[self.arrNews objectAtIndex:index];
[self hideGradientBackground:newsView.webViewDetailedNews];
[newsView downloadImage];
[newsView loadWebView];
// NSLog(#"cell=%d ,arr image index=%#",index,[self.arrNews objectAtIndex:index]);
return cell;
}

iPhone - Poor Performance of a UITableView with a UIScrollView of UIImageViews in each cell

I have a UITableViewCell that contains a UIScrollView. The UIScrollView contains a different number of UIImageViews for each row. Some rows have 5 images in the scroll view, some rows have 15 images in the scroll view. Since the number of UIImageViews in each row is different, I'm stuck allocating new UIImageViews when each row is drawn! This is obviously having a negative impact on scroll performance of my UITableView. How can I resolve this to improve UITableView scrolling?
Additionally, using Instruments' Time Profiler, I've noticed that the method call [tableView dequeueReusableCellWithIdentifier:MyIdentifier] is abnormally slow. Does that help point to the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"HuddleTableCell";
HuddleListItemTableViewCell *cellToReturn = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cellToReturn == nil) {
[[NSBundle mainBundle] loadNibNamed:#"HuddleListItemTableViewCell" owner:self options:nil];
cellToReturn = cell;
UILabel *badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
badgeLabel.font = [UIFont boldSystemFontOfSize:16];
badgeLabel.numberOfLines = 2;
badgeLabel.textAlignment = UITextAlignmentCenter;
badgeLabel.textColor = [UIColor whiteColor];
badgeLabel.backgroundColor = [UIColor clearColor];
[cellToReturn.attendee4Image addSubview:badgeLabel];
cellToReturn.othersOverlayLabel = badgeLabel;
[badgeLabel release];
cellToReturn.heartHolderView.layer.cornerRadius = 5;
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:.29 green:.39 blue:.57 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.19 green:.22 blue:.36 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:.102 green:.122 blue:.17 alpha:1.0] CGColor], nil];
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0x4A59A8) CGColor], nil];
cellToReturn.heartHolderView.layer.cornerRadius = 5.0;
UIView *polaroidView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 39)];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)];
[polaroidView addSubview:img];
[cellToReturn.organizerImage addSubview:polaroidView];
cellToReturn.polaroidView = polaroidView;
cellToReturn.polaroidImageView = img;
[img release];
[polaroidView release];
cellToReturn.gradientView.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:.96 green:.96 blue:.96 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.85 green:.85 blue:.85 alpha:1.0] CGColor], nil];
cellToReturn.votingTimerLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventDateLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventNameLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numInviteesLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numVotesLabel.textColor = UIColorFromRGB(0x616163);
self.cell = nil;
}
NSString *organizerFbId = [[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"userId"];
[cellToReturn.polaroidImageView setImageWithURL:[self.organizerImageUrls objectForKey:organizerFbId] placeholderImage:self.placeholderImage];
[[cellToReturn.imageScrollViewHolder subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
if ([self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]) {
[cellToReturn.imageScrollViewHolder addSubview:[self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]];
} else {
UIScrollView *tempImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 46)];
tempImageScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *fbUids = [[self.huddleList objectAtIndex:indexPath.row] objectForKey:#"facebookInvitees"];
int i=0;
for (i=0;i < [fbUids count];i++) {
UIView *polaroidView = [[[UIView alloc] initWithFrame:CGRectMake((i * 45) + (3 * i), 0, 37, 39)] autorelease];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)] autorelease];
[img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", [fbUids objectAtIndex:i]]] placeholderImage:self.placeholderImage];
[polaroidView addSubview:img];
[tempImageScrollView addSubview:polaroidView];
}
[tempImageScrollView setContentSize:CGSizeMake((i*45) + (3 * i), 46)];
[self.avatarScrollViews setObject:tempImageScrollView forKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]];
[cellToReturn.imageScrollViewHolder addSubview:tempImageScrollView];
[tempImageScrollView release];
}
return cellToReturn;
}
Even there was a fixed number of images in each cell's scroller, you'd be stuck allocating them as the cells are dequeued and reused. The way to get speed is to take up some more space, by keeping the cells' subviews in an array outside of the table.
Create another array that has the same count as your huddleList array. Fill that array with UIScrollViews containing the imageViews. All that code in your cellForRowAtIndexPath method can move to the initialization method for the new array and get replaced by a few simple lines:
// dequeue or build a cell
// tag the scroll views so you can find them
UIScrollView *oldScrollView = (UIScrollView *)[cell viewWithTag:kCELL_SCROLL_VIEW];
// this might be a reused cell, so remove the scroll view if it has one
if (oldScrollView) [oldScrollView removeFromSuperview];
// now add the one that you setup for this cell
// your init code that used to be in this method sets up the frame, the tag, etc
UIScrollView *currentScrollView = [self.cachedScrollViews objectAtIndex:indexPath.row];
[cell addSubview:currentScrollView];
// that's it
Don't forget to release the cachedScrollViews on memory warning

iOS Overlay View - Masking UITableViewController with a view is partially masking UITableViewController

iPhone - 5.1(9B176)
I have been encountering inconsistent behaviour in overlaying a mask view on top of a UITableViewController as shown in picture below. Anyone encountered this behaviour, if yes, please suggest any solutions used?
Below is the code i have used to add mask view:
//Add the overlay view.
if(ovController == nil) {
NSString *cellIdentifier = #"overlayVC";
ovController = [[self storyboard] instantiateViewControllerWithIdentifier:cellIdentifier];
}
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.alpha = 0.85;
ovController.delegate = self;
UIView *aboveView = self.parentViewController.view;
[self.tableView insertSubview:ovController.view aboveSubview:aboveView];
I have also tried
`[self.tableView insertSubview:ovController.view aboveSubview:self.tableView]`
Hi use something like below code
-(void) showLoadingView {
//NSLog(#">>>>>>>>>>>>>>>>> loading view Landscape right ");
if (loadView == nil) {
loadView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadView.opaque = NO;
loadView.backgroundColor = [UIColor clearColor];
//loadView.alpha = 0.8;
viewBack = [[UIView alloc] initWithFrame:CGRectMake(95, 230, 130, 40)];
viewBack.backgroundColor = [UIColor blackColor];
viewBack.alpha = 0.7f;
viewBack.layer.masksToBounds = NO;
viewBack.layer.cornerRadius = 8;
spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5.0, 5.0, 30.0, 30.0)];
[spinningWheel startAnimating];
// spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[viewBack addSubview:spinningWheel];
[spinningWheel release];
UILabel *lblLoading = [[UILabel alloc] initWithFrame:CGRectMake(23, 6, 110, 25)];
lblLoading.backgroundColor = [UIColor clearColor];
lblLoading.font = [UIFont fontWithName:#"Helvetica-Bold" size:16.0];
lblLoading.textAlignment = UITextAlignmentCenter;
lblLoading.textColor = [UIColor whiteColor];
lblLoading.text = #"Loading...";
[viewBack addSubview:lblLoading];
[loadView addSubview:viewBack];
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
loadView.frame = iphoneFrame;
viewBack.frame = loadiPhone;
}
else
{
loadView.frame = ipadFrame;
viewBack.frame = loadiPad;
}
[self.window addSubview:loadView];
}
-(void) hideLoadingView {
[loadView removeFromSuperview];
}
declare this methods in you APPdelegate class so you can call it from any view in your code.
here i have set a view over the window not in view this surelu solve your problem!!!!

custom cells with drawRect: and setNeedsDisplay methods

there are 3 settings, in my app, that can be change the drawing of a cell.
By default, a cell of my table view show the name and the cost of an object... Changing these 3 setting, an user can choose to show a description or a link insted of cost.
I wrote a lot of code and now, my app can change cell's drawing without quit from it...
My problem is the drawing is changed only for new objects added but old objects don't change!
How can I do to change also the old cell's drawing without quit from app?
This is the code of my cell (i'm using setNeedsDisplay and drawRect methods):
#import "WishTableCell.h"
#implementation WishTableCell
#synthesize wish;
#synthesize imageView;
#synthesize nomeLabel;
#synthesize label;
#synthesize costoLabel;
#synthesize linkDescLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 28, 28)];
imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:imageView];
nomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 8, 235, 22)];
[self.contentView addSubview:nomeLabel];
if ([NSLocalizedString(#"CostoCella", #"") isEqualToString:#"Costo:"]) {
label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 40, 16)];
}
else {
label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 35, 16)];
}
[self.contentView addSubview:label];
if ([NSLocalizedString(#"CostoCella", #"") isEqualToString:#"Costo:"]) {
costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 28, 185, 16)];
}
else {
costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(93, 28, 195, 16)];
}
[self.contentView addSubview:costoLabel];
linkDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 235, 16)];
[self.contentView addSubview:linkDescLabel];
self.backgroundView = [[UIImageView alloc] init];
UIImage *rowBackground = [UIImage imageNamed:#"cellBg.png"];
((UIImageView *)self.backgroundView).image = rowBackground;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)setWish:(Wish *)newWish {
if (newWish != wish) {
[wish release];
wish = [newWish retain];
}
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
NSLog(#"DrawRect called!");
nomeLabel.text = wish.nome;
nomeLabel.font = [UIFont boldSystemFontOfSize:18.0];
nomeLabel.textColor = [UIColor colorWithRed:0.039 green:0.4 blue:0.737 alpha:1.0];
nomeLabel.textAlignment = UITextAlignmentLeft;
nomeLabel.shadowColor = [UIColor whiteColor];
nomeLabel.shadowOffset = CGSizeMake(0, 1);
nomeLabel.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = NSLocalizedString(#"CostoCella", #"");
label.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
label.textAlignment = UITextAlignmentLeft;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0, 1);
label.backgroundColor = [UIColor clearColor];
costoLabel.font = [UIFont boldSystemFontOfSize:12.0];
costoLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
costoLabel.textAlignment = UITextAlignmentLeft;
costoLabel.shadowColor = [UIColor whiteColor];
costoLabel.shadowOffset = CGSizeMake(0, 1);
costoLabel.backgroundColor = [UIColor clearColor];
linkDescLabel.font = [UIFont boldSystemFontOfSize:12.0];
linkDescLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0];
linkDescLabel.textAlignment = UITextAlignmentLeft;
linkDescLabel.shadowColor = [UIColor whiteColor];
linkDescLabel.shadowOffset = CGSizeMake(0, 1);
linkDescLabel.backgroundColor = [UIColor clearColor];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"costoView"]) {
linkDescLabel.hidden = YES;
label.hidden = NO;
costoLabel.hidden = NO;
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Euro"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"€ %#", wish.costo];
costoLabel.text = costo;
}
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Dollaro"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"$ %#", wish.costo];
costoLabel.text = costo;
}
if ([[defaults objectForKey:#"valutaCosto"] isEqualToString:#"Sterlina"]) {
NSString *costo = [[NSString alloc] initWithFormat:#"£ %#", wish.costo];
costoLabel.text = costo;
}
}
else if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"descrizioneView"]) {
label.hidden = YES;
costoLabel.hidden = YES;
linkDescLabel.hidden = NO;
linkDescLabel.text = wish.descrizione;
}
else if ([[defaults objectForKey:#"dettagliView"] isEqualToString:#"urlView"]) {
label.hidden = YES;
costoLabel.hidden = YES;
linkDescLabel.hidden = NO;
linkDescLabel.text = wish.link;
}
if (wish.categoria == nil)
imageView.image = [UIImage imageNamed:#"personale.png"];
if ([wish.categoria isEqualToString:#"Abbigliamento"])
imageView.image = [UIImage imageNamed:#"abbigliamento.png"];
else if ([wish.categoria isEqualToString:#"Casa"])
imageView.image = [UIImage imageNamed:#"casa.png"];
else if ([wish.categoria isEqualToString:#"Cibo"])
imageView.image = [UIImage imageNamed:#"cibo.png"];
else if ([wish.categoria isEqualToString:#"Divertimento"])
imageView.image = [UIImage imageNamed:#"divertimento.png"];
else if ([wish.categoria isEqualToString:#"Elettronica"])
imageView.image = [UIImage imageNamed:#"elettronica.png"];
else if ([wish.categoria isEqualToString:#"Hobby"])
imageView.image = [UIImage imageNamed:#"hobby.png"];
else if ([wish.categoria isEqualToString:#"Internet"])
imageView.image = [UIImage imageNamed:#"internet.png"];
else if ([wish.categoria isEqualToString:#"Regali"])
imageView.image = [UIImage imageNamed:#"regali.png"];
else if ([wish.categoria isEqualToString:#"Ufficio"])
imageView.image = [UIImage imageNamed:#"ufficio.png"];
else if ([wish.categoria isEqualToString:#"Viaggi"])
imageView.image = [UIImage imageNamed:#"viaggi.png"];
else if ([wish.categoria isEqualToString:#"Personale"])
imageView.image = [UIImage imageNamed:#"personale.png"];
}
- (void)dealloc {
[wish release];
[imageView release];
[nomeLabel release];
[costoLabel release];
[linkDescLabel release];
[label release];
[super dealloc];
}
#end
Thanks a lot for the attention!
Matthew
Try to call [NSTableView reloadData] function, and set the drawing properties in the delegate method (cellForRowAtIndexPath).
I think that you also can use interface to create the cell (.xib), then you do not need write so many assign property code....