UIVIew中的UITableView在滚动时冻结重新加载

UIVIew中的UITableView在滚动时冻结重新加载

问题描述:

我有一个UIViewController,它有一个UITableView。我想有一个显示文本的搜索栏,并且有一个按钮,可以选择带有选项的UIPickerView。当我从UIPickerView中选择任何选项时,tableView会重新加载。有两个问题我注意到目前为止:UIVIew中的UITableView在滚动时冻结重新加载

  1. 我的tableView的每个单元格覆盖约。身高400px;当我滚动tableView并将其放置在单元格位于半路/半路以外的位置时,我重新加载表格0行,整个应用程序冻结,并且XCode不记录任何错误

  2. 当我拉起机械手并做出选择,同时使实现代码如下背景滚动,应用程序再次冻结,这不记录任何错误

在这两种情况下,应用程序不会崩溃(不退出),但冻结了UI,使其他交互成为不可能。任何形式的帮助,将不胜感激。由于

以下是代码: - 代码自定义搜索栏 `

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    _leftMonthButton=[[UIButton alloc] initWithFrame:CGRectMake(leftArrowX, 0, leftArrowWidth, 40)]; 
    _leftMonthButton.imageView.contentMode=UIViewContentModeScaleAspectFit; 
    [_leftMonthButton setImage:[UIImage imageNamed:@"arrow-left.png"] forState:UIControlStateNormal]; 
    [_leftMonthButton addTarget:self action:@selector(monthSwitched:) forControlEvents:UIControlEventTouchUpInside]; 


    _rightMonthButton=[[UIButton alloc] initWithFrame:CGRectMake(rightArrowX, 0, rightArrowWidth, 40)]; 
    _rightMonthButton.imageView.contentMode=UIViewContentModeScaleAspectFit; 
    [_rightMonthButton setImage:[UIImage imageNamed:@"arrow-right.png"] forState:UIControlStateNormal]; 
    [_rightMonthButton addTarget:self action:@selector(monthSwitched:) forControlEvents:UIControlEventTouchUpInside]; 

    _dropDownButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
    _dropDownButton.frame=CGRectMake(downArrowX, 0, downArrowWidth, 40); 
    _dropDownButton.imageView.contentMode=UIViewContentModeScaleAspectFit; 
    [_dropDownButton setImage:[UIImage imageNamed:@"downArrow.png"] forState:UIControlStateNormal]; 
    [_dropDownButton addTarget:self action:@selector(dropDownButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    _activeMonthIndex=1; 

    [self addSubview:_leftMonthButton]; 
    [self addSubview:_rightMonthButton]; 
    [self addSubview:_dropDownButton]; 

    CGRect frame=_dropDownButton.frame; 
    frame=CGRectMake(downArrowX, 0, downArrowWidth, 40); 
    _dropDownButton.frame=frame; 
} 


(void)dropDownButtonClicked:(UIButton*)sender 
{ 

    [self.searchBarDelegate brandPickerClicked]; 
} 

(void)monthSwitched:(UIButton*)sender 
{ 
    if (sender==_leftMonthButton) 
    { 
     if (_activeMonthIndex>0) 
     { 
      if (_rightMonthButton.alpha==0) _rightMonthButton.alpha=1; 
      _activeMonthIndex-=1; 
      _monthLabel.text=_activeMonths[_activeMonthIndex]; 
     } 
     if (_activeMonthIndex==0) 
     { 
      _leftMonthButton.alpha=0; 
     } 
    } 
    else 
    { 
     if (_activeMonthIndex<2) 
     { 
      if (_leftMonthButton.alpha==0) _leftMonthButton.alpha=1; 
      _activeMonthIndex+=1; 
      _monthLabel.text=_activeMonths[_activeMonthIndex]; 
     } 
     if (_activeMonthIndex==2) 
     { 
      _rightMonthButton.alpha=0; 
     } 
    } 
    [self.searchBarDelegate monthChangedTo:_monthLabel.text]; 
}` 

-Code用于实施的tableView的搜索栏

` - (无效)viewDidLoad中 { [超级viewDidLoad中]。 //在加载视图后执行其他任何设置。

_searchBar=[[CalendarSearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)]; 
    _searchBar.searchBarDelegate=self; 
    [_headerView addSubview:_searchBar]; 

    self.calendarTable.dataSource=self; 
    self.calendarTable.delegate=self; 
    self.calendarTable.allowsSelection=false; 

    UIView *tableViewFooter=[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 23)]; 
    tableViewFooter.backgroundColor=[UIColor colorwithHexString:@"333333" alpha:1]; 

    [self.calendarTable setTableFooterView:tableViewFooter]; 

    _calendar=[CalendarData sharedReleaseData]; 
    _currentTableRowItems=_calendar; 

    CGFloat screenHeight=[UIScreen mainScreen].bounds.size.height; 
    CGFloat screenWidth=[UIScreen mainScreen].bounds.size.width; 

    _picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, screenHeight-400, screenWidth, 400.0)]; 
    _picker.backgroundColor=[UIColor colorwithHexString:@"666666" alpha:1]; 
    _picker.dataSource=self; 
    _picker.delegate=self; 
} 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return _currentTableRowItems.count; 
    } 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    calendarCell *cell = (CalendarCell*)[tableView dequeueReusableCellWithIdentifier:@"calendarCell" forIndexPath:indexPath]; 

    CalendarObject *object=_currentTableRowItems[indexPath.row]; 

    cell.backgroundColor=[UIColor colorwithHexString:@"333333" alpha:1]; 
    cell.calendarCardView.backgroundColor=[UIColor whiteColor]; 
    return cell; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [UIScreen mainScreen].bounds.size.height*479/568; 
} 

// Picker Controller Delegate Methods 
-(void)pickerClicked 
{ 
    if (![_picker isDescendantOfView:self.view]) 
    { 
     [self.calendarTable setContentOffset:CGPointZero animated:YES]; 
     [self.calendarTable setScrollEnabled:NO]; 
     [self.view addSubview:_picker]; 
    } 
} 

-(void)updateTableViewObjectsWithSearchString:(NSString*)searchString andSearchContext:(SearchContext)searchContext 
{ 
    [_currentTableRowItems removeAllObjects]; 

    if (searchContext==date) 
    { 
      for (CalendarObject *object in _calendar.calendarArray) 
      { 
       if ([object.date.text compare:searchString options:NSCaseInsensitiveSearch]==NSOrderedSame && [object.monthName compare:_searchBar.monthLabel.text options:NSCaseInsensitiveSearch]==NSOrderedSame) [_currentTableRowItems addObject:object]; 
      } 
     } 
    } 

    dispatch_async(dispatch_get_main_queue(), ^(void){ 
     [self.releaseCalendarTable reloadData]; 
    }); 

    // [self.calendarTable setContentOffset:CGPointZero animated:YES]; 
} 

//Data Source Picker View 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return _searchBar.dates.count; 
} 

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *title = _searchBar.dateArray[row]; 
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{ 
                            NSForegroundColorAttributeName: [UIColor whiteColor], 
                            NSFontAttributeName: [UIFont fontWithName:@"UnitedSansRegTT-Bold" size:18.0f], 
                            }]; 
    return attString; 
} 

//PickerView Delegate 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
     inComponent:(NSInteger)component 
{ 
    [self.calendarTable setScrollEnabled:YES]; 
    _searchBar.label.text=_searchBar.brandNames[row]; 
    [self updateTableViewObjectsWithSearchString:_searchBar.label.text andSearchContext:date]; 
    [self.picker removeFromSuperview]; 
} 

`

+0

发布您的整个代码,所以我们可以看到问题的地方 –

+0

我有添加了代码,请让我知道如果你需要任何解释 – user3602624

我觉得我得到了你的问题,你要添加的drawrect.Don't子视图做画矩形是只用于绘制特定view.This是一个非常不好的做法,它在initwithframe移动或者如果一个笔尖连接在awakefromnib中执行,不要添加和删除pickerview,在里面创建一个视图将pickerview和隐藏/显示它

+0

你想让我为pickerView创建一个单独的视图它保留在内存中? – user3602624

+0

你必须明白,创建和销毁用户界面隐藏和显示它们是非常昂贵的,而当视觉效果不明显时,视图会占用更多内存。 –