简体   繁体   English

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

[英]UITableView in UIVIew freezes on reload while scrolling

I have a UIViewController that has a UITableView in it. 我有一个包含UITableView的UIViewController。 I wanted to have a Search bar which displays text and has Button that pulls up a UIPickerView with options. 我想要一个显示文本的搜索栏,并具有一个可以拉起带有选项的UIPickerView的按钮。 when I select any option from the UIPickerView the tableView reloads. 当我从UIPickerView中选择任何选项时,tableView将重新加载。 There are two problems that I have noticed so far: 到目前为止,我注意到了两个问题:

  1. Each cell of my tableView covers approx. 我的tableView的每个单元格覆盖大约。 400px in height; 高400像素; when I scroll the tableView and leave it in a position where the cell is halfway/little more than halfway out, and I reload the table with 0 rows the entire app freezes, and the XCode does not record any error 当我滚动tableView并将其放置在单元格位于一半以上的位置时,我用0行重新加载表格,整个应用程序冻结,并且XCode不会记录任何错误

  2. When I pull up the Picker and make a selection while making the tableview in the background scroll, the app freezes again, which not recording any error 当我拉起选择器并在后台滚动查看表格时进行选择时,应用再次冻结,这不会记录任何错误

In either case, the app does not crash(does not quit), but freezes the UI making any other interaction impossible. 无论哪种情况,该应用程序都不会崩溃(不会退出),但是会冻结UI,使其他任何交互都无法进行。 Any kind of help would be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

Following is the code: - Code for custom Search bar ` 以下是代码:-自定义搜索栏的代码`

- (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 for tableView implementing the searchBar -实现searchBar的tableView的代码

` - (void)viewDidLoad { [super viewDidLoad]; `-(void)viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. //加载视图后进行其他任何设置。

    _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];
}

` `

我想我遇到了问题,您正在drawrect上添加子视图。不要那样做的rect rect仅用于绘制特定的视图。这是一个非常糟糕的做法,将其移动到initwithframe中,或者如果连接了笔尖,则在awakefromnib中将其移动并不要不要添加和删除pickerview,在其中创建一个视图将是pickerview并隐藏/显示它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM