Iphone TableView磁头分区内存分配
我正在使用Iphone TableView磁头分区内存分配,iphone,objective-c,memory-management,tableview,Iphone,Objective C,Memory Management,Tableview,我正在使用 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 24.0)];//custom view // create the button object headerLabel
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 24.0)];//custom view
// create the button object
headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width, 18)];//header label
headerLabel.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"section" ofType: @"png"]]];
customView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"section" ofType: @"png"]]];
headerLabel.textColor = [UIColor whiteColor];//text color of header label
headerLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15];//font of header label
headerLabel.font = [UIFont boldSystemFontOfSize:15];
headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 24.0);//frame of header label
if(searching)//checking searching {
headerLabel.text = @" Search Results";//searching results as a text of header label
}
else {
if(section == 0)//checking which section is chosen {
headerLabel.text = @" Sponsored";//sponsored as header label
}
else {
headerLabel.text = @" Unsponsored";//un sponsored as header label
}
}
if(searching)//checking searching {
headerLabel.text = @" Sök resultat";//sok resultant text as a header label
}
else {
if(section == 0)//checking which section is chosen {
headerLabel.text = @" Sponsrade";//sponsorade as text of header label
}
else {
headerLabel.text = @" Un Sponsrade";//usponsrade as text of header label
}
}
[customView addSubview:headerLabel];//header label as a subview
//NSLog(@"endOfviewForHeaderInSection");
//[headerLabel release];
return customView;//returning custom view
[customView release];
//NSLog(@"endOfviewForHeaderInSection");
}
在表格视图的标题部分将表格分为两部分,当我上下滚动表格视图时,内存分配增加得非常快,但在标题部分没有使用上述代码,内存分配没有,应用程序工作正常。但需要在标题部分使用上述代码,我无法从标题中删除代码,我还想减少内存分配。是否有其他方法解决此问题
提前感谢。您的文章中的代码格式不好,很难理解 但我看到的是:您可能正在泄漏customView,因为您在发布它之前返回了它。您可以在返回之前自动释放它
如果这是表的顶部,一种可能是使用表的headerView,我相信您不需要像viewForHeaderView部分那样重复分配。您也可以尝试创建和缓存新的,而不是反复创建。请查看您发布的代码格式!
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//NSLog(@"startOfviewForHeaderInSection");
// create the parent view that will hold header Label
customView = nil;
[customView release];
headerLabel =nil;
[headerLabel release];
customView = [[UIView alloc]init];
headerLabel = [[UILabel alloc]init];
// customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 24.0)];//custom view
// create the button object
// headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width, 18)];//header label
//customView.frame = CGRectMake(0.0, 0.0, 320.0, 24.0);
//headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 24.0);//frame of header label
//DetailedCoupon *detailedcouponObj = [[DetailedCoupon alloc]init];//object of detailed
headerLabel.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"section" ofType: @"png"]]];
//customView.backgroundColor = [detailedcouponObj getColor:@"D7D8D1"];//background color
customView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"section" ofType: @"png"]]];
//headerLabel.backgroundColor = [UIColor blackColor];
// headerLabel.opaque = NO;//opaque header label
headerLabel.textColor = [UIColor whiteColor];//text color of header label
// headerLabel.highlightedTextColor = [UIColor whiteColor];//highlighted text color of header label
// headerLabel.font = [UIFont boldSystemFontOfSize:20];//font of header label
headerLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15];//font of header label
headerLabel.font = [UIFont boldSystemFontOfSize:15];
headerLabel.frame = CGRectMake(0.0, 0.0, 320.0, 24.0);//frame of header label
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];//object of NSUserDefault
NSString *storeLanguage = [pref objectForKey:@"language"];//store language of string type
if ([storeLanguage isEqualToString:@"English"])//comparison of language choosed
{
if(searching)//checking searching
{
headerLabel.text = @" Search Results";//searching results as a text of header label
}
else {
if(section == 0)//checking which section is choosed
{
headerLabel.text = @" Sponsored";//sponsored as header label
}
else
{
headerLabel.text = @" Unsponsored";//un sponsored as header label
}
}
}
else
{
if(searching)//checking searching
{
headerLabel.text = @" Sök resultat";//sok resultant text as a header label
}
else {
if(section == 0)//checking which section is choosed
{
headerLabel.text = @" Sponsrade";//sponsorade as text of header label
}
else
{
headerLabel.text = @" Un Sponsrade";//usponsrade as text of header label
}
}
}
[customView addSubview:headerLabel];//header label as a subview
//NSLog(@"endOfviewForHeaderInSection");
//[headerLabel release];
storeLanguage = nil;
[storeLanguage release];
return customView;//returning custom view
[pref release];
[customView release];
//NSLog(@"endOfviewForHeaderInSection");
}