Skip to content

Instantly share code, notes, and snippets.

@farcaller
Created August 22, 2009 05:16
Show Gist options
  • Save farcaller/172637 to your computer and use it in GitHub Desktop.
Save farcaller/172637 to your computer and use it in GitHub Desktop.
-(void)loadScrollView
{
[[NSUserDefaults standardUserDefaults] setObject:self.dataSourceArray
forKey:@"data"];
[[NSUserDefaults standardUserDefaults] synchronize];
for (NSInteger i=0; i < [self.dataSourceArray count]; i++)
{
NSString *textString = [[self.dataSourceArray objectAtIndex:i]
objectForKey:kLabelTextKey];
// ^^ это медленно, используй fast enumeration: for(xx in yy)
CGSize currentSize = [textString sizeWithFont:[UIFont
fontWithName:@"Georgia" size:18]];
CGRect labelFrame = CGRectMake(64, i*24+(i+1)*20, currentSize.width,
24);
UILabel *textLabel = [[UILabel alloc] initWithFrame:labelFrame];
textLabel.text = textString;
textLabel.font = [UIFont fontWithName:@"Georgia" size:18];
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.tag = i+1000;
[scrollView addSubview:textLabel];
// и тут textLabel утекает... [textLabel release];
CGSize scrollSize = scrollView.contentSize;
if (currentSize.width + 70 > scrollSize.width)
{
scrollView.contentSize = CGSizeMake(currentSize.width + 70, 376);
}
}
//Done and Cross button init
CGRect buttonFrame = CGRectMake(0, 0, 0, 0);
CGRect crossFrame = CGRectMake(0, 0, 0, 0);
CGRect timeFrame = CGRectMake(0, 0, 0, 0);
for (NSInteger i=0; i < [self.dataSourceArray count]; i++)
{
if (i == 0)
{
buttonFrame = CGRectMake(34, 20, 30, 30);
}else {
buttonFrame = CGRectMake(34, i*30+(i+1)*15, 30, 30);
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
if ([[[[NSUserDefaults standardUserDefaults] objectForKey:@"data"]
objectAtIndex:i] objectForKey:kDoneKey] == [NSString
stringWithString:@"1"])
// ^^ сравнил два разных указателя, fail. Тут надо isEqualToString:
// да и [NSString stringWithString:@"1"] -- это зло какое-то, @"1" чем не угодил?
{
[button setBackgroundImage:[UIImage imageNamed:@"check.png"]
forState:UIControlStateNormal];
if (i == 0)
{
crossFrame = CGRectMake(5, 24, 21, 21);
}else {
crossFrame = CGRectMake(5, i*20+(i+1)*24, 21, 21);
}
UIButton *cross = [UIButton buttonWithType:UIButtonTypeCustom];
cross.tag = i+3000;
cross.frame = crossFrame;
[cross setBackgroundImage:[UIImage imageNamed:@"cross.png"]
forState:UIControlStateNormal];
[cross addTarget:self action:@selector(thingDelete:)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:cross];
// ^^ а тут лика нет, cross на пулле
NSString *timeText = [NSString stringWithFormat:@"Done in %@",
[[self.dataSourceArray objectAtIndex:i]
objectForKey:kTimePresentKey]];
CGSize timeSize = [timeText sizeWithFont:[UIFont
fontWithName:@"Helvetica" size:14]];
if (i == 0)
{
timeFrame = CGRectMake(128, 40, timeSize.width, 18);
}else {
timeFrame = CGRectMake(128, i*44+40, timeSize.width, 18);
}
UILabel *timeLabel = [[UILabel alloc] initWithFrame:timeFrame];
timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
timeLabel.textColor = [UIColor grayColor];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.text = timeText;
timeLabel.tag = i+4000;
[scrollView addSubview:timeLabel];
// ^^ а тут снова есть
CGSize scrollSize = scrollView.contentSize;
if (timeSize.width + 130 > scrollSize.width)
{
scrollView.contentSize = CGSizeMake(timeSize.width + 130, 376);
}
}else {
[button setBackgroundImage:[UIImage imageNamed:@"uncheck.png"]
forState:UIControlStateNormal];
}
button.tag = i+2000;
button.frame = buttonFrame;
[button addTarget:self action:@selector(thingDone:)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
if ([self.dataSourceArray count] > 8)
{
CGSize scrollSize = scrollView.contentSize;
CGFloat heightFloat = [self.dataSourceArray count]*47;
scrollView.contentSize = CGSizeMake(scrollSize.width, heightFloat);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment