UITableView - Как оставить оправдать? - PullRequest
0 голосов
/ 19 сентября 2011

Я пытаюсь понять, как выравнивать по левому краю записи в UITableView. Я искал исходный код из главы 8 книги Вей-Мэн Ли «Начало разработки приложений для iOS 4 (WROX)».

The running app looks like this

Источник выглядит так:

TableViewExample.h

//
//  TableViewExampleViewController.h
//  TableViewExample
//
//  Created by Wei-Meng Lee on 5/18/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TableViewExampleViewController : UIViewController 
    <UITableViewDataSource, UITableViewDelegate> {      

}

@end

TableViewExample.m

//
//  TableViewExampleViewController.m
//  TableViewExample
//
//  Created by Wei-Meng Lee on 5/18/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "TableViewExampleViewController.h"

@implementation TableViewExampleViewController

NSMutableArray *listOfMovies;

//---insert individual row into the table view--- 
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell";

    //---try to get a reusable cell--- 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //---create new cell if no reusable cell is available--- 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    //---set the text to display for the cell--- 
    NSString *cellValue = [listOfMovies objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue;

    //---display an image---
    UIImage *image = [UIImage imageNamed:@"apple.jpeg"];
    cell.imageView.image = image;   

    return cell;    
}

//---set the number of rows in the table view---
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return [listOfMovies count];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    //---initialize the array--- 
    listOfMovies = [[NSMutableArray alloc] init];

    //---add items--- 
    [listOfMovies addObject:@"Training Day"]; 
    [listOfMovies addObject:@"Remember the Titans"]; 
    [listOfMovies addObject:@"John Q."]; 
    [listOfMovies addObject:@"The Bone Collector"];
    [listOfMovies addObject:@"Ricochet"]; 
    [listOfMovies addObject:@"The Siege"]; 
    [listOfMovies addObject:@"Malcolm X"]; 
    [listOfMovies addObject:@"Antwone Fisher"];
    [listOfMovies addObject:@"Courage Under Fire"];
    [listOfMovies addObject:@"He Got Game"]; 
    [listOfMovies addObject:@"The Pelican Brief"]; 
    [listOfMovies addObject:@"Glory"]; 
    [listOfMovies addObject:@"The Preacher's Wife"];
    [super viewDidLoad];
}

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section{ 
    //---display "Movie List" as the header---
    return @"Movie List";
}

- (NSString *)tableView:(UITableView *)tableView 
titleForFooterInSection:(NSInteger)section {
    //---display "by Denzel Washington" as the footer--- 
    return @"by Denzel Washington";
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *movieSelected = [listOfMovies objectAtIndex:indexPath.row];
    NSString *msg = [NSString stringWithFormat:@"You have selected %@", movieSelected];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Movie selected" 
                                                    message:msg
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show]; 
    [alert release]; 
}

- (NSInteger)tableView:(UITableView *)tableView 
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [indexPath row] % 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;  
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

@end

Я пытался (без успеха) изменить UITableView, чтобы записи оставались выровненными. Я прочитал много сообщений о UITableViews, и я могу найти только настройки для выравнивания по левому краю при редактировании. Как настроить его только для просмотра и прокрутки?

Спасибо за помощь ...

1 Ответ

3 голосов
/ 19 сентября 2011

Удалить это:

- (NSInteger)tableView:(UITableView *)tableView 
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [indexPath row] % 2;
}

Этот метод делегата вызывается для каждой строки, и код здесь возвращает значение 1 для каждой второй строки.

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