【发布时间】:2014-08-06 04:23:31
【问题描述】:
我是 stackoverflow 的新手,对 iOS 开发比较陌生。我对UITableView 的重新加载有疑问,这让我发疯。在选择UITableView 的任何行时,表格视图会向上移动一点。我已经在didSelect 上重新加载了表格视图,即每当用户点击UITableViewCell 时。我使用 autolayout 来计算 UITableViewCell 的行高。但是,每当单击表格视图行时,表格视图都会滚动一点,即表格视图内容会突然向上移动。这是我的代码..
#pragma mark UITableView DELEGATES AND DATASOURCE
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;//enter code here
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr_display count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//-----COMMENTED AUTOLAYOUT OUT TO TEST------HEIGHT IS KEPT STATICto 40-----
[self configureCell:_stubCell atIndexPath:indexPath];
[_stubCell layoutSubviews];
// [_stubCell needsUpdateConstraints];
// [_stubCell setNeedsLayout];
CGFloat height = [_stubCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height + 1;
//setNeedsUpdateConstraints
// return 40;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40.f;
}
- (void)configureCell:(cell_detail_search *)cell atIndexPath:(NSIndexPath *)indexPath
{
//cell.lbl.text = _tableData[indexPath.row % _tableData.count];
// NSDictionary *dict=[[[[self.arr_display objectAtIndex:indexPath.row] objectAtIndex:1] valueForKey:@"info"] objectAtIndex:0];
cell.lbl_skills.text=[NSString stringWithFormat:@"%@",[self.arr_display objectAtIndex:indexPath.row]];
if ([self.arr_selected_search_list containsObject:[NSString stringWithFormat:@"%@",[self.arr_display objectAtIndex:indexPath.row]]])//-----GIVING TICK TO THE CELL
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType=UITableViewCellAccessoryNone;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell_detail_search *cell = [tableView1 dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (cell==nil)
{
cell =[[cell_detail_search alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.arr_selected_search_list containsObject:[self.arr_display objectAtIndex:indexPath.row] ])
{
// [self.arr_id_list removeObjectAtIndex:indexPath.row];
[self.arr_selected_search_list removeObjectAtIndex:[self.arr_selected_search_list indexOfObject:[self.arr_display objectAtIndex:indexPath.row] ]];
self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];
}
else
{
[self.arr_selected_search_list addObject:[self.arr_display objectAtIndex:indexPath.row] ];
self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];
NSLog(@"self.arr_id_list =%@",self.arr_selected_search_list);
}
[self.tbl_search_details reloadData];
}
#pragma mark scrollview delegate made for tableview pagination---------
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if ([str_heading isEqualToString:@"Skills"])
{
float diff=self.tbl_search_details.contentSize.height-self.tbl_search_details.frame.size.height;
if (diff<0)
{
diff=0;
}
float drag_distance=self.tbl_search_details.contentOffset.y-diff;
NSLog(@"drag distance %f",drag_distance);
if (drag_distance>80)
{
[self.activity_pagination startAnimating];
[self determining_the_api_to_call_with_str:self.str_heading];
}
}
如果有人可以帮助我,我将非常感激.....提前谢谢
【问题讨论】:
-
看看你的代码,我明白你是如何努力寻找错误的:s
标签: ios iphone objective-c uitableview tableview