iOS: выдвинуть массив в UITableView - PullRequest
0 голосов
/ 16 февраля 2011

Можете ли вы провести меня по шагам, чтобы передать массив в UITableview?

Я новичок в Cocoa / Objective-C, поэтому вы можете объяснить, как .m, .h работают с делегатом

Ответы [ 2 ]

3 голосов
/ 16 февраля 2011

Сделайте это:

@interface SomeInstance : UITableViewController {
    NSArray *theArray;
}

@end

@implementation SomeInstance


- (void)viewDidLoad {
    theArray = [[NSArray alloc] initWithObjects:@"Apple",@"Pineapple",@"Banana",nil];
    [super viewDidLoad];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [theArray count];
}



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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected a row" message:[theArray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}


- (void)dealloc {
    [theArray release];
    [super dealloc];
}


@end
1 голос
/ 16 февраля 2011

Если вы новичок в ObjC, я бы посоветовал начать с чего-то более простого, прежде чем углубляться в UITableViews.

(извините, если вы этого не хотите слышать, но я думаю, вы будете более продуктивнымив долгосрочной перспективе).

Чтобы ответить на ваш конкретный вопрос, ожидается, что класс, который наследуется от UITableViewControllerDelegate, будет поддерживать различные методы, требуемые протоколом UITableViewController.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...