2013-05-02 2 views
0

내 tableView에서 셀을 선택하고 셀이 강조 표시되면 셀이 강조 표시 될 때 테이블을 스크롤 할 때 그 이유를 필사적으로 시도합니다. "Arts and Museums"또는 "Coffee and Bakeries"를 예로 들자면 -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 2] 오류가 발생합니다. 두 범주 모두 3 가지 값을 포함하므로 [0 .. 2]을 얻을 수는 있지만, 왜 그런 일이 발생하는지 이해할 수 없습니다. 콘솔에서 볼 수있는 것처럼 앱이로드되면 총 10 개의 셀 (각 카테고리 당 1 개)이 생성됩니다. 컴파일러 하이라이트이 라인 오류가 발생하면 아래 cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row]; 코드 ...NSMutableArray로 작업 할 때 "경계 너머"오류가 발생했습니다.

@interface PreViewController() 

{ 
    NSMutableArray *arrayNo; 
} 

@end 

@implementation PreViewController 

-(void)viewDidAppear:(BOOL)animated 
{ 

NSString *arts = @"Arts and Museums"; 
NSString *coffee = @"Coffee and Bakeries"; 
NSString *tours = @"Tours and Festivals"; 
NSString *hotels = @"Hotels and Inns"; 
NSString *leisure = @"Leisure and Recreation"; 
NSString *music = @"Live Music"; 
NSString *bars = @"Night Clubs and Bars"; 
NSString *food = @"Restaurants"; 
NSString *shopping = @"Shopping"; 
NSString *transportation = @"Transportation"; 

[arrayNo addObject:arts]; 
[arrayNo addObject:coffee]; 
[arrayNo addObject:tours]; 
[arrayNo addObject:hotels]; 
[arrayNo addObject:leisure]; 
[arrayNo addObject:music]; 
[arrayNo addObject:bars]; 
[arrayNo addObject:food]; 
[arrayNo addObject:shopping]; 
[arrayNo addObject:transportation]; 


    [[self myTableView] reloadData]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    arrayNo = [[NSMutableArray alloc] init]; 
    [[self myTableView] setDelegate:self]; 
    [[self myTableView] setDataSource:self]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [arrayNo count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) 
    { 
     NSLog(@"CREATING NEW CELL"); 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0]; 
    } 

    cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

if (cell) 
{ 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reLoad)]; 
    tapped.numberOfTapsRequired = 1; 
    [cell addGestureRecognizer:tapped]; 
} 

if ([cell.textLabel.text isEqualToString: @"Arts and Museums"]) 
{   
    NSString *galleries = @"Art Galleries"; 
    NSString *dramatic = @"Dramatic Arts"; 
    NSString *museums = @"Museums"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:galleries]; 
    [arrayNo addObject:dramatic]; 
    [arrayNo addObject:museums]; 
} 

if ([cell.textLabel.text isEqualToString: @"Coffee and Bakeries"]) 
{ 
    NSString *bakeries = @"Bakeries"; 
    NSString *cafes = @"Cafés"; 
    NSString *shops = @"Coffee Shops"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:bakeries]; 
    [arrayNo addObject:cafes]; 
    [arrayNo addObject:shops]; 
} 
if ([cell.textLabel.text isEqualToString: @"Tours and Festivals"]) 
{ 
    NSString *festivals = @"Food and Drink Festivals"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:festivals];   
} 
if ([cell.textLabel.text isEqualToString: @"Hotels and Inns"]) 
{ 
    NSString *breakfasts = @"Bed and Breakfasts"; 
    NSString *hotels = @"Hotels"; 
    NSString *inns = @"Inns"; 
    NSString *motels = @"Motels"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:breakfasts]; 
    [arrayNo addObject:hotels]; 
    [arrayNo addObject:inns]; 
    [arrayNo addObject:motels]; 
} 
if ([cell.textLabel.text isEqualToString: @"Leisure and Recreation"]) 
{ 
    NSString *arcades = @"Arcades"; 
    NSString *beaches = @"Beaches"; 
    NSString *bowling = @"Bowling"; 
    NSString *breweries = @"Breweries"; 
    NSString *campgrounds = @"Campgrounds"; 
    NSString *cinemas = @"Cinemas"; 
    NSString *climbing = @"Climbing"; 
    NSString *parks = @"Parks"; 
    NSString *ski = @"Ski Resorts"; 
    NSString *spa = @"Spa Resorts"; 
    NSString *water = @"Water Rentals"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:arcades]; 
    [arrayNo addObject:beaches]; 
    [arrayNo addObject:bowling]; 
    [arrayNo addObject:breweries]; 
    [arrayNo addObject:campgrounds]; 
    [arrayNo addObject:cinemas]; 
    [arrayNo addObject:climbing]; 
    [arrayNo addObject:parks]; 
    [arrayNo addObject:ski]; 
    [arrayNo addObject:spa]; 
    [arrayNo addObject:water]; 
} 
if ([cell.textLabel.text isEqualToString: @"Live Music"]) 
{ 
    NSString *bars = @"Bars"; 
    NSString *clubs = @"Clubs"; 
    NSString *restaurants = @"Restaurants"; 
    NSString *theaters = @"Theaters"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:bars]; 
    [arrayNo addObject:clubs]; 
    [arrayNo addObject:restaurants]; 
    [arrayNo addObject:theaters]; 
} 
if ([cell.textLabel.text isEqualToString: @"Night Clubs and Bars"]) 
{ 
    NSString *bars = @"Bars"; 
    NSString *lounges = @"Lounges"; 
    NSString *clubs = @"Night Clubs"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:bars]; 
    [arrayNo addObject:lounges]; 
    [arrayNo addObject:clubs]; 
} 
if ([cell.textLabel.text isEqualToString: @"Restaurants"]) 
{ 
    NSString *asian = @"Asian"; 
    NSString *fast = @"Fast Food"; 
    NSString *french = @"French"; 
    NSString *german = @"German"; 
    NSString *grill = @"Grill and Variety"; 
    NSString *indian = @"Indian"; 
    NSString *italian = @"Italian"; 
    NSString *mexican = @"Mexican"; 
    NSString *eastern = @"Middle Eastern"; 
    NSString *seafood = @"Seafood"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:asian]; 
    [arrayNo addObject:fast]; 
    [arrayNo addObject:french]; 
    [arrayNo addObject:german]; 
    [arrayNo addObject:grill]; 
    [arrayNo addObject:indian]; 
    [arrayNo addObject:italian]; 
    [arrayNo addObject:mexican]; 
    [arrayNo addObject:eastern]; 
    [arrayNo addObject:seafood]; 
} 
if ([cell.textLabel.text isEqualToString: @"Shopping"]) 
{ 
    NSString *art = @"Art Supplies"; 
    NSString *books = @"Books"; 
    NSString *candy = @"Candy"; 
    NSString *cooking = @"Cooking"; 
    NSString *electronics = @"Electronics"; 
    NSString *apparel = @"Apparel"; 
    NSString *florists = @"Florists"; 
    NSString *grocery = @"Grocery"; 
    NSString *health = @"Health"; 
    NSString *home = @"Home"; 
    NSString *jewlery = @"Jewelry"; 
    NSString *music = @"Music"; 
    NSString *outdoor = @"Outdoor Gear"; 
    NSString *photography = @"Photography"; 
    NSString *souvenirs = @"Souvenirs"; 
    NSString *sports = @"Sports"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:apparel]; 
    [arrayNo addObject:art]; 
    [arrayNo addObject:books]; 
    [arrayNo addObject:candy]; 
    [arrayNo addObject:cooking]; 
    [arrayNo addObject:electronics]; 
    [arrayNo addObject:florists]; 
    [arrayNo addObject:grocery]; 
    [arrayNo addObject:health]; 
    [arrayNo addObject:home]; 
    [arrayNo addObject:jewlery]; 
    [arrayNo addObject:music]; 
    [arrayNo addObject:outdoor]; 
    [arrayNo addObject:photography]; 
    [arrayNo addObject:souvenirs]; 
    [arrayNo addObject:sports]; 
} 
if ([cell.textLabel.text isEqualToString: @"Transportation"]) 
{ 
    NSString *airports = @"Airports"; 
    NSString *bicycle = @"Bicycle Rentals"; 
    NSString *bus = @"Bus Lines"; 
    NSString *ferries = @"Ferries"; 
    NSString *taxis = @"Taxis"; 
    NSString *trains = @"Trains"; 
    NSString *rentals = @"Vehicle Rentals"; 

    [arrayNo removeAllObjects]; 

    [arrayNo addObject:airports]; 
    [arrayNo addObject:bicycle]; 
    [arrayNo addObject:bus]; 
    [arrayNo addObject:ferries]; 
    [arrayNo addObject:taxis]; 
    [arrayNo addObject:trains]; 
    [arrayNo addObject:rentals]; 
} 

} 나는 .. 나는이 GestureRecognizer 제거, 그냥 얻을 말할 것을 생각한다면

- (void) reLoad 
{ 
    [self.myTableView reloadData]; 
} 

답변

0

didSelectRowAtIndexPath 메소드의 마지막 문장으로 배열의 내용을 변경 한 후 reloadData를 수행하십시오. reloadData는 절대로 호출되지 않기 때문에이 문제가 발생할 수 있습니다. numberOfRowsInSection에 새로운 호출이 없으므로 테이블에 여전히 10 개의 행이 있다고 생각합니다.

제스처를하기 전에 제스처 인식기를 &에 추가해야하며 didSelectRowAtIndexPath가 있으므로 표 셀에는 필요하지 않습니다.

+0

고마워요! 나는 그것을 조사 할 것이다. 내 유일한 생각은 적어도 뒤로 탐색 (doubletap)을 위해 GestureRecognizer를 사용하고 싶지만, 아마도 대안을 탐색 할 것입니다. – Greg

관련 문제