0

NSMutableArray(self.tablePdfListArray)在同一个索引处插入tableview textlabel和插入。它首先被正确添加,但是当我再次打开时,正在变成。NSMutableArray(self.dateListArray)detailtextlabelTableViewdetailTextlabeltextlabeltextlabeldetailTextlabel

NSLog两者都有,NSMutabelArray并且知道两个数组值都在交换。如何保持其原有的价值?提前感谢您的任何建议。 使用 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;

 }
 }
4

2 回答 2

1

你为什么要检查tableView == pdfListnameTable

那应该是tableView isEqual:self. pdfListnameTable。不确定这是否相关,但如果你有多个 tableView - 我猜你没有切换到它,因为似乎缺少 else 声明。

于 2013-06-20T07:16:22.543 回答
0

好吧,我不确定,但我对您的代码做了一些重构。您在某些地方看起来像是在尝试访问属性,但随后您也尝试将其作为实例值进行访问。

所以,这就是我所做的。它可能不正确。但它应该很接近(或者至少会帮助你解决这个问题)

@interface someTableViewController()
@property(nonatomic, strong) NSMutableArray *tablePdfListArray;
@property(nonatomic, strong) NSMutableArray *dateListArray;
@property(nonatomic, copy) NSString *myPDFName;
@property(nonatomic, strong) NSDate *firstDayInYear;
@property(nonatomic, strong) NSDateFormatter *dateFormatter;
@property(nonatomic, weak) IBOutlet UITableView *pdfListnameTable;

@end


@implementation someTableViewController

-(void)viewDidLoad {

    self.tablePdfListArray = [NSMutableArray new];
    self.dateListArray = [NSMutableArray new];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if([userDefaults objectForKey:@"children"] != nil ) {
        self.tablePdfListArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"children"]];
    }

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

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; //cell bg

    NSInteger currentRow = indexPath.row;

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

    NSString *tablePdfdate = [self.dateListArray objectAtIndex:currentRow];
    cell.detailTextLabel.text = tablePdfdate;

    UIButton *someButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [someButton setTitle:@"CLICK" forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(testButtonClickIndexPath:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:someButton];

    return cell;
}
-(void)testButtonClickIndexPath:(id)sender {

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.pdfListnameTable];
    NSIndexPath *indexPath = [self.pdfListnameTable indexPathForRowAtPoint:touchPoint];

    if(indexPath != nil) {
        // show alert message, call it, or whatever.  just using a silly one for now..
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"RAR"
                            message:@"Mamma Say..my..my mamma say"
                                                  delegate:self
                                         cancelButtonTitle:@"Medulla Oblongata"
                                         otherButtonTitles:@[ @"h2o", @"Gatorade"]];
        [alert show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if(buttonIndex == 0) {

        self.myPDFName = [NSString stringWithFormat:@"%@", [alertView textFieldAtIndex:0].text];
        self.firstDayInYear = [NSDate date];

        self.dateFormatter = [[NSDateFormatter alloc] init];
        [self.dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];

        NSString *currentTime = [self.dateFormatter stringFromDate:self.firstDayInYear];

        NSLog(@"User's current time in their preference format:%@",currentTime);


        [self.dateListArray insertObject:currentTime atIndex:0];

        NSLog(@"mhy date dateListArray %@",self.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 %@",self.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:@"dates"]];
            [defaults setObject:self.tablePdfListArray forKey:[NSString stringWithFormat:@"children"]];
            [defaults synchronize];
        }
    }
}

@end
于 2013-06-20T20:45:33.963 回答