2010-08-16 8 views
-2

배열에서 선택한 데이터를로드하고 사용자 선택에 따라 변경하는 iphone 탭을 만드는 방법.iphone 즐겨 찾기 탭 만들기

+0

가능합니다. 그러나 귀하의 질문은 너무 모호합니다. –

답변

1

ALX는

간단한 시나리오는 만들 수있을 것 NIB 기반 UITableViewCell (이것 밖에 자습서의 많음이있다) 어떤 종류의 그것에 레이블이있다. 당신은 사용자가 셀을 선택하고 다음NSUserDefaults에 저장됩니다 변경 가능한 배열로 그 저장할 때 다음 라벨의 내용을 잡고있다 수행 할 수있는

.

그런 다음 다른보기에서 NSUserDefaults에 액세스하여 묻는 것처럼 "즐겨 찾기"탭을 채울 수 있습니다.

약간의 샘플 코드가 도움이됩니다 (이전 질문에서 말한 것처럼 원래 데이터가 UITableView에 있다고 가정). 이 코드를 내 머리 부분 (테스트되지 않은 코드)에서 작성하므로 모든 버그를 해결해야하지만 아이디어는 정확합니다. 새 뷰 컨트롤러에 일단

// in the .h file 
#import <UIKit/UIKit.h> 

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    // set up a mutable array which allows editing of the array 
    NSMutableArray *myFavoritesData; 

} 
// set up a retained property 
@property (nonatomic, retain) NSMutableArray *myFavoritesData; 

@end 


// in the .m file 
// synthesize the getters/setters for your array 
@synthesize myFavoritesData; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // find cell that was just pressed 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    // get pointer for the label that we want to identify the cell by 
    // the tag in this case is set to '5' in Interface Builder in the options for the label 
    UILabel *someLabel; 
    someLabel = (UILabel *)[cell viewWithTag:5]; 

    NSString *tmpFavorite = someLabel.text; 

    // get the count of the current array and use that for the "new" row since the count 
    // will always be 1 larger than the last object in the array (arrays start at 0, counts start at 1) 
    NSUInteger newRow = [self.myFavoritesData count]; 
    [self.myFavoritesData insertObject:tmpFavorite atIndex:newRow]; 

} 

// save the mutable array into NSUserDefaults when the view is about to disappear 
- (void) viewWillDisappear:(BOOL)animated 
{ 

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    [userDefaults setObject:self.myFavoritesData forKey:@"MyFavorites"]; 

    // synchronize the data now instead of waiting for the OS to synchronize it at some 
    // arbitrary time in the future 
    [userDefaults synchronize]; 

} 

, 당신은 단지 NSUserDefaults 읽고 배열에서 테이블을 채울 수 있습니다. 예를 들어 :

// favorites view controller 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init]; 
    tmpArray = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy]; 

     if ([tmpArray count] == 0) { 

      // 
      // no favorites have ever been saved 
      // 


     } else { 

      // load the favorites into some array you synthesized just like before 
      self.tableFavoritesData = [[NSMutableArray alloc] init]; 
      self.tableFavoritesData = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy]; 

      NSLog(@"favorites data is %d and %@", [self.tableFavoritesData count], self.tableFavoritesData); 

} 

[tmpArray release]; 
} 

그런 다음 즐겨 찾기의 cellForRowAtIndexPath 방금 ​​배열의 각 인덱스에 각 문자열에 액세스 컨트롤러를 볼 당신 (그래서, 인덱스 0에 대한 문자열이 행 0으로 갈 것에, 인덱스 1 문자열로 갈 것 1 행 등) 그리고 이것은 당신이 좋아하는 테이블을 채울 것입니다!

사용해보세요.

+0

덕분에 많은 인스턴트 메신저의 구조를 얻었습니다. 하나의 작은 문제 임에도 불구하고 세포 내용물을 얻고 싶지 않습니다. 나는 셀에 의해 보여지는 뷰의 내용을 얻고 싶다. 이 코드를 변경해야 할 것 같습니다. UILabel * someLabel; someLabel = (UILabel *) [cell viewWithTag : 5]; NSString * tmpFavorite = someLabel.text; ? 나는 ([[rootArray objectAtIndex : indexPath] set]; 을 사용하려고했으나 실제로는 작동하지 않았다. :( –

+0

) 어떻게'rootArray'가 정의 되었는가? 위의 변경 가능한 배열을 어떻게 정의했는지와 비슷한 경우, '[self.rootArray objectAtIndex.indexPath.row];''indexPath.row'는 int이고 눌려진 현재 행을 지정합니다. – iwasrobbed

관련 문제