2013-01-06 127 views
0

我的tableview变化值。它似乎使用表中其他部分的相同值。我想象它在我的单元格创建中出现错误,这里是代码。 请告诉我我做错了什么,谢谢!值改变滚动我的tableview的时候,当我向上或向下滚动

EDITED ADDED整个代码

//global indexpath to remember which cell tapped 
NSIndexPath *globalPath; 
@interface SearchViewController() 

@end 

@implementation SearchViewController 

//Load implementation once per launch 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self linkInputTableToDelegate]; 
    _temporaryResultsArray =[[NSMutableArray alloc]init]; 
    _flurryArray=[[NSMutableArray alloc]init]; 
    _numberOfSections=6; 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:NO]; 
    [InputTable reloadData]; 

    textFromUserDefaults=[[[HelperMethods alloc]init]getObjectUserDefault:@"textFiltered"]; 
    [self addTextToFlurryArrayForFlurryAndSavedLists:_textFromUserDefaults]; 



} 
-(void)viewDidDisappear:(BOOL)animated{ 

} 

- (IBAction)searchButtonPressed:(UIButton *)sender { 
    self.tabBarController.selectedIndex = 1; 
} 


//Makes the input table respond to delegate table view methods 
-(void)linkInputTableToDelegate{ 
    _inputTable.dataSource=self; 
    _inputTable.delegate=self; 
} 

-(void)performSearch:(NSString*)text{ 
    //do search 
} 

#pragma mark - Table view data source 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    int numberOfRows=_numberOfSections; 

    //Rows for iPhone 4 
    if ([[UIScreen mainScreen]bounds].size.height==480) { 
     numberOfRows=numberOfRows; 
     //Rows for iPhone 5 
    }else if ([[UIScreen mainScreen]bounds].size.height==568){ 
     numberOfRows=numberOfRows+1; 
    } 
    return numberOfRows; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //In reality groups are created with 1 row inside, this is to allow spacing between the rows 

    return 1; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *kCellID = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID]; 
    } 

    //Is the cell the same as the one clicked when going to ingredient filter 
    BOOL cellIndexPathSameAsSelected=[self isCellIndexSameAsPreviousClicked:indexPath]; 

    cell.textLabel.textColor=[UIColor blackColor]; 
    if (cellIndexPathSameAsSelected && _textFromUserDefaults!=nil) { 

     if (![cell.textLabel.text isEqualToString:_textFromUserDefaults]) { 

      cell.textLabel.text=_textFromUserDefaults; 
      [self performTextSearch:_textFromUserDefaults]; 
     } 

    } 
    return cell; 
} 

//Compares the previous clicked cell with the cell now selected 
-(BOOL)isCellIndexSameAsPreviousClicked: (NSIndexPath*)cellPath{ 

    if (cellPath.row == globalPath.row && globalPath.section==cellPath.section) { 
     return YES; 
    } 
    else{ 
     return NO; 
    } 
} 

- (void)updateTableViewWithExtraRow :(NSIndexPath*)rowSelected{ 
    NSLog(@"number of sections =%i",_numberOfSections); 
    if (rowSelected.section == _numberOfSections) { 
     _numberOfSections ++; 
    } 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; 
    [[[HelperMethods alloc]init]saveObjectToUserDefaults:cellText :@"textFiltered"]; 
    globalPath = indexPath; 
    [self updateTableViewWithExtraRow:indexPath]; 
} 

-(void)addTextToFlurryArrayForFlurryAndSavedLists:(NSString*)text{ 
    if ([_flurryArray count]==0 &&[text length]>0) { 
     [_flurryArray addObject:text]; 
    } 
    for (int i=0;i<[_flurryArray count];i++) { 
     NSString *textInArray=[_flurryArray objectAtIndex:i]; 
     if (![textInArray isEqualToString:text]) { 
      [_flurryArray addObject:text]; 
     } 

    } 
    NSLog(@"Total number of saved items = %i",[_flurryArray count]); 
} 

// Dispose of any resources that can be recreated. 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 
+0

你需要记住这一点以往任何时候都实现代码如下细胞是可重复使用的。所以,你需要节省了店面并在单元格对用户可见时重新填充值。 –

+0

我该怎么做? –

回答

1

问题是你是只有在满足这些条件时才更改单元格中的文本。例如,当您的cellIndexPathSameAsSelectedNO时,可以不更改单元格。所以你应该添加一个else并做一些设置。

编辑:

if (cellIndexPathSameAsSelected && _textFromUserDefaults!=nil) { 

    if (![cell.textLabel.text isEqualToString:_textFromUserDefaults]) { 

     cell.textLabel.text=_textFromUserDefaults; 
     [self performTextSearch:_textFromUserDefaults]; 
    } 

} else { 
    cell.textLabel.text = [NSString string]; 
} 
+0

也许我做错了,但是当我点击一个细胞,我去到一个新的VC(文本过滤器),选择一个匹配它的文本(用户默认)这里返回后,所以我只想填充单元格如果从过滤器文本,或者如果它是相同的文字(在取消的情况下是在加压过滤) –

+0

我不知道我得到它。所以,你有多个小区,用户再次点其中之一,确实在下一屏幕一些事情,之后返回到这个画面,你想比以前减少显示细胞?因为在这种情况下你会想改变你的数据源,所以'numberOfRowsInSection:'方法返回单元格的确切数量。只填写你需要的东西并不是一件好事,剩下的就放在那里。 – Levi

+0

有5行这里,当用户点击他所挑选的字符串(用字的过滤器)的行。在离开前,我保存点击单元格的索引。当用户从过滤器返回一个字符串时返回。然后我想在我们离开之前将该字符串放在同一行中以过滤配料。 (表是每个实际上很多路段有1行之间,允许单元格间距) –

0

[tableView dequeueReusableCellWithIdentifier:kCellID];通话结束后,你必须检查,如果你的电池,其实是可以重复使用或不使用如下语句:

if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:...]; 
} 
0

你应该做这样的事情

static NSString *kCellID = @"Cell"; 

// Acquire the cell if possible. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 
if (cell == nil) // Not possible to re-use cell, so create a new cell 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID]; 
} 
相关问题