ios - 两个 nsmutablearray 值在 ios 中与另一个值交换

标签 ios objective-c uitableview nsmutablearray

我正在插入NSMutableArray(self.tablePdfListArray)tableview textlabelNSMutableArray(self.dateListArray)detailtextlabel在相同的索引处。它首先被正确添加,但是当我打开 TableView 时再次detailTextlabel成为textlabeltextlabel正在成为detailTextlabel .

我有NSLog两者都是NSMutabelArray并知道两个数组值都在交换。如何保留其原有的值(value)?预先感谢您的任何建议。 使用tableView代码编辑

 - (void)viewDidLoad
 {
 if([[NSUserDefaults standardUserDefaults] objectForKey:@"children"] != nil )
{
    self.tablePdfListArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"children"]];

}
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"dates"] != nil)
{
    self.dateListArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"dates"]];

}
}
  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
  self.myPDFName = [NSString stringWithFormat:@"%@", [alertView textFieldAtIndex:0].text];

    firstDayInYear = [NSDate date];
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *currentTime = [dateFormatter stringFromDate:firstDayInYear];
    NSLog(@"User's current time in their preference format:%@",currentTime);

    if(!self. tablePdfListArray)
    {

        self.tablePdfListArray = [[NSMutableArray alloc]init];
    }
    if(!self.dateListArray)
    {
        self.dateListArray = [[NSMutableArray alloc]init];
    }
    [self.dateListArray insertObject:currentTime atIndex:0];

    NSLog(@"mhy date dateListArray %@",dateListArray);

     //the below if condition will not allow repeatative string array in tableList and textfield lenth.
           if ([[alertView textFieldAtIndex:0].text length] != 0 && ![self.tablePdfListArray containsObject:self.myPDFName])
    {

        [self.tablePdfListArray insertObject:[NSString stringWithFormat:@"%@", [alertView textFieldAtIndex:0].text] atIndex:0];

                   NSLog(@"mhy date tablePdfListArray %@",tablePdfListArray);
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        [self.pdfListnameTable insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setObject:self.dateListArray forKey:[NSString stringWithFormat:@"children"]];
        [defaults setObject:self.tablePdfListArray forKey:[NSString stringWithFormat:@"dates"]];
        [defaults synchronize];

    }
  }}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if( tableView == pdfListnameTable)
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;           //cell bg
        //self.myChklist.backgroundColor = [UIColor clearColor];

    }

    NSString *tablePdfname = [self.tablePdfListArray objectAtIndex:indexPath.row];
    cell.textLabel.text = tablePdfname;

    NSString *tablePdfdate = [self.dateListArray objectAtIndex:indexPath.row];
            //[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];



       cell.detailTextLabel.text = tablePdfdate;

    return cell;

 }
 }

最佳答案

为什么要检查tableView == pdfListnameTable

那应该是tableView isEqual:self。 pdfListnameTable。不确定这是否与这里相关,但如果您有多个 tableView - 我猜您不会切换到它,因为似乎缺少一个 else 语句。

关于ios - 两个 nsmutablearray 值在 ios 中与另一个值交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17206936/

相关文章:

ios - 如何通过点击改变UILabel的内容?

ios - CABasicAnimation 不会为我的不透明度设置动画

ios - 如何在非ARC模式下手动释放此静态变量

ios - 在 UITableView 的自定义单元格中使用 slider

iphone - 如何使添加到 UITableViewCell 内容 View 的 UIView 适合其范围?

ios - 对 SKShapeNode 施加脉冲

xcode - 同一个ipa支持armv6和armv7

ios - 为 iPhone 编写后台进程时出错

ios - 具有不同可选部分的 UITableView?

ios - 表达式中的 NSExpression 自定义变量