2013-06-20 4 views
0

tableview textlabel에 넣고 NSMutableArray(self.dateListArray)detailtextlabel에 같은 인덱스에 삽입합니다. 처음에 올바르게 추가되었지만 TableView을 다시 열면 detailTextlabeltextlabel으로되고 textlabeldetailTextlabel이됩니다.두 개의 nsmutablearray 값이 서로 다른 값으로 바뀌고 있습니다.

나는 NSLog 모두 NSMutabelArray이며 배열 값이 모두 바뀌고 있음을 알게되었습니다. 원래 값을 유지하는 방법? 제안에 대해 미리 감사드립니다. 은 왜 tableView == pdfListnameTable을 확인하는있는 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; 

} 
} 
+1

값이 바뀌는 것을 보여주는 나머지 부분은 어디에서입니까? –

+1

cellForRowAtIndexPath 메소드에 대한 코드를 게시하십시오. – Girish

+0

확인. 코드를 추가했습니다. –

답변

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 
+0

나는 이것을 용서합니다. 하지만 내 타이핑 오류가 버그 위에 발생합니다. 수정은 다음과 같습니다 [기본값 setObject : self.dateListArray forKey : [NSString stringWithFormat : @ "dates"]]; [기본값 setObject : self.tablePdfListArray forKey : [NSString stringWithFormat : @ "children"]]; –

+0

forKey 개체를 잘못 작성했습니다. 나는 당신의 해결책도 시도했다. 귀중한 노력에 감사드립니다. 위의 수정을 사용하여 답변을 편집 할 수 있습니까? 다른 사용자도이 내용을 배울 수 있도록 허용합니다. –

+0

굉장한, 다행 이군요. 위의 의견을 업데이트했습니다. - 감사합니다. –

1

으로 편집?

tableView isEqual:self. pdfListnameTable이어야합니다. 여기에 관련이 있는지 모르겠지만 하나 이상의 tableView가있는 경우 - 그 대신에 else 문이 없어 보이는 것처럼 전환하지 않는 것으로 추측 할 수 있습니다.

+0

+1, 전체 코드 읽기 용 : –

+0

확인. tableView == pdfListnameTable을 제거했습니다. 예 불필요한 상태 인 것 같습니다. 아직 결과가 없습니다. –

+0

+1에 감사드립니다. 아래에 약간의 리팩토링 기능을 가진 별도의 메시지를 추가했습니다. 실제 문제가 무엇인지 명확하게 파악하고, 모든 것을 정의하는 방법과 함께 ... –

관련 문제