Created
November 20, 2012 11:34
-
-
Save CodeIQ/4117416 to your computer and use it in GitHub Desktop.
20121122_1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MemoryLeakTestTableViewController.m | |
// MemoryLeakTest | |
#import "ASIHTTPRequest.h" | |
@interface MemoryLeakTest2ViewController : UITableViewController | |
{ | |
NSMutableArray *_dataForCellArray0; | |
} | |
@property (retain, nonatomic) NSMutableArray *dataForCellArray1; | |
@property (retain, nonatomic) NSMutableArray *dataForCellArray2; | |
@property (retain, nonatomic) NSArray *dataForCellArray3; | |
@property (retain, nonatomic) NSArray *dataForCellArray4; | |
@property (retain, nonatomic) NSMutableArray *sections; | |
@property (retain, nonatomic) UIImage *image; | |
@end | |
@implementation MemoryLeakTest2ViewController | |
@synthesize dataForCellArray1; | |
@synthesize dataForCellArray2; | |
@synthesize dataForCellArray3; | |
@synthesize dataForCellArray4; | |
@synthesize sections; | |
@synthesize image; | |
- (void)dealloc | |
{ | |
[_dataForCellArray0 release]; | |
self.dataForCellArray1 = nil; | |
self.dataForCellArray2 = nil; | |
self.dataForCellArray3 = nil; | |
self.dataForCellArray4 = nil; | |
self.sections = nil; | |
self.image = nil; | |
[super dealloc]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
NSMutableArray *dataForCellArray = [NSMutableArray array]; | |
for (int i = 0; i < 10; i++) { | |
[dataForCellArray addObject:[NSString stringWithFormat:@"Cell Row %d", i]]; | |
} | |
if (_dataForCellArray0 == nil) { | |
_dataForCellArray0 = [dataForCellArray mutableCopy]; | |
} | |
self.dataForCellArray1 = [[dataForCellArray mutableCopy] autorelease]; | |
self.dataForCellArray2 = [[dataForCellArray mutableCopy] autorelease]; | |
self.dataForCellArray3 = [[[NSArray alloc] initWithArray:dataForCellArray] autorelease]; | |
self.dataForCellArray4 = [[[NSArray alloc] initWithArray:dataForCellArray] autorelease]; | |
self.sections = [[[NSMutableArray alloc] init] autorelease]; | |
for (int i = 0; i < 4; i++) { | |
[self.sections addObject:[NSString stringWithFormat:@"dataForCellArray%d", i]]; | |
} | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
[self performSelectorInBackground:@selector(getImageInBackground) withObject:nil]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
} | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return [self.sections count]; | |
} | |
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section | |
{ | |
return [self.sections objectAtIndex:section]; | |
} | |
- (void)showAlert { | |
UIAlertView* alert=[[[UIAlertView alloc] initWithTitle:@"Oops" message:@"Connection Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; | |
[alert show]; | |
} | |
- (void)getImageInBackground | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://snapdi.sh/images/snapdish_logo1.png"]]; | |
[request startSynchronous]; | |
NSError *error = [request error]; | |
if (error) { | |
[self showAlert]; | |
} else { | |
self.image = [UIImage imageWithData:[request responseData]]; | |
[self.tableView reloadData]; | |
} | |
[pool release]; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
if (section == 0) { | |
return [_dataForCellArray0 count]; | |
} else if (section == 1) { | |
return [self.dataForCellArray1 count]; | |
} else if (section == 2) { | |
return [self.dataForCellArray2 count]; | |
} else if (section == 3) { | |
return [self.dataForCellArray3 count]; | |
} else if (section == 4) { | |
return [self.dataForCellArray4 count]; | |
} | |
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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
if (indexPath.section == 0) { | |
cell.textLabel.text = [_dataForCellArray0 objectAtIndex:indexPath.row]; | |
} else if (indexPath.section == 1) { | |
cell.textLabel.text = [self.dataForCellArray1 objectAtIndex:indexPath.row]; | |
} else if (indexPath.section == 2) { | |
cell.textLabel.text = [self.dataForCellArray2 objectAtIndex:indexPath.row]; | |
} else if (indexPath.section == 3) { | |
cell.textLabel.text = [self.dataForCellArray3 objectAtIndex:indexPath.row]; | |
} else if (indexPath.section == 4) { | |
cell.textLabel.text = [self.dataForCellArray4 objectAtIndex:indexPath.row]; | |
} | |
if (self.image != nil) { | |
cell.imageView.image = self.image; | |
[[cell.contentView viewWithTag:1] removeFromSuperview]; | |
UIImageView *imageView = [[[UIImageView alloc] initWithImage:self.image] autorelease]; | |
imageView.frame = CGRectMake(280, 10, 30, 30); | |
imageView.tag = 1; | |
[cell.contentView addSubview:imageView]; | |
} | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment