Ios 滚动我的tableview时,值会发生变化

Ios 滚动我的tableview时,值会发生变化,ios,uitableview,Ios,Uitableview,我的tableview在向上或向下滚动时更改值。它似乎使用了表中其他部分的相同值。我想这是我创建单元格时的一个错误,下面是代码。 请告诉我我做错了什么,谢谢 编辑添加了整个代码 //global indexpath to remember which cell tapped NSIndexPath *globalPath; @interface SearchViewController () @end @implementation SearchViewController //Load

我的tableview在向上或向下滚动时更改值。它似乎使用了表中其他部分的相同值。我想这是我创建单元格时的一个错误,下面是代码。 请告诉我我做错了什么,谢谢

编辑添加了整个代码

//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];
}
//全局索引XPath以记住哪个单元格被点击
nsindepath*globalPath;
@接口SearchViewController()
@结束
@实现SearchViewController
//每次启动加载一次实现
-(无效)viewDidLoad
{
[超级视图下载];
[自链接输入到删除];
_临时结果数组=[[NSMutableArray alloc]init];
_flurryArray=[[NSMutableArray alloc]init];
_截面数=6;
}
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:否];
[可输入的重新加载数据];
textFromUserDefaults=[[HelperMethods alloc]init]getObjectUserDefault:@“textFiltered”];
[self addTextToFlurryArrayForFlurryandSavedList:_textFromUserDefaults];
}
-(无效)视图消失:(BOOL)已设置动画{
}
-(iAction)搜索按钮按下:(UIButton*)发件人{
self.tabBarController.selectedIndex=1;
}
//使输入表响应委托表视图方法
-(无效)链接InputableToDelegate{
_可输入。数据源=自身;
_可输入。代表=自我;
}
-(无效)性能搜索:(NSString*)文本{
//搜索
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
int numberOfRows=\u numberOfSections;
//iPhone4的行数
如果([[UIScreen mainScreen]bounds].size.height==480){
numberOfRows=numberOfRows;
//iphone5的行数
}else if([[UIScreen mainScreen]bounds].size.height==568){
numberOfRows=numberOfRows+1;
}
返回numberOfRows;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//实际上,创建的组中有一行,这是为了允许行之间的间距
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*kCellID=@“单元格”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:kCellID];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:kCellID];
}
//该单元格是否与进入成分过滤器时单击的单元格相同
BOOL-cellindexpath-sameaselected=[self-isCellIndexSameAsPreviousClicked:indexath];
cell.textlab.textColor=[UIColor blackColor];
如果(CellIndExpathSameaselected&&U textFromUserDefaults!=nil){
如果(![cell.textLabel.text IsequalString:\u textFromUserDefaults]){
cell.textlab.text=\u textFromUserDefaults;
[自执行文本搜索:_textFromUserDefaults];
}
}
返回单元;
}
//将以前单击的单元格与现在选定的单元格进行比较
-(BOOL)isCellIndexSameAsPreviousClicked:(NSIndexPath*)单元格路径{
if(cellPath.row==globalPath.row&&globalPath.section==cellPath.section){
返回YES;
}
否则{
返回否;
}
}
-(void)UpdateTableViewWithExtraw:(NSIndexPath*)行已选中{
NSLog(@“节数=%i”,节数);
if(rowSelected.section==\u numberOfSections){
_numberOfSections++;
}
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSString*cellText=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[[[HelperMethods alloc]init]saveObjectToUserDefaults:cellText:@“textFiltered”];
globalPath=indepath;
[self-UpdateTableViewWithExtraw:indexPath];
}
-(无效)将文本添加到FlurryArrayForFlurryandSavedList:(NSString*)文本{
如果([\u flurryArray count]==0&&[text length]>0){
[[u flurryArray addObject:text];
}

对于(int i=0;i在您的
[tableView dequeueReusableCellWithIdentifier:kCellID];
调用之后,您必须使用以下语句检查您的单元格是否可以实际重用:

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

问题是,您仅在满足这些条件时才更改单元格中的文本。例如,当您的
cellindexpathsameaselected
NO
时,您使单元格保持不变。因此,您应该添加
else
,并在那里进行一些设置

编辑:

if (cellIndexPathSameAsSelected && _textFromUserDefaults!=nil) {

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

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

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

你应该这样做

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

你需要时刻牢记tableview单元格是可重用的。因此,每当用户看到单元格时,你需要保存存储并重新填充值。也许我做得不对,但当我单击单元格时,我会进入一个新的VC(文本过滤器),在选择匹配项后,它会返回该文本(在用户默认情况下)因此,我只想在有来自过滤器的文本,或者是相同的文本(如果过滤器中按了取消)的情况下填充单元格我不确定我是否理解。因此,您有许多单元格,用户点击其中一个,在下一个屏幕中执行一些操作,在再次返回此屏幕后,您希望显示的单元格比以前少?因为在这种情况下,您希望更改数据源,因此
numberOfRowsInSection:
方法返回确切的单元格数ls.只填充您需要的内容,而保留其余内容不是一件好事。这里有5行,当用户单击一行时,他将被带去选择字符串(使用单词过滤器)。在离开之前,我保存单击单元格的索引。当用户返回时,会从filter返回一个字符串。然后,我想在我们离开前将该字符串放在同一行,以过滤成分。(表中实际上有许多部分,每个部分之间有一行,以允许单元格之间的间距)如果你认为有帮助的话,我可以发布整个内容,所以你希望其他单元格保持空白(除了点击的单元格)?看看我的编辑。