2011-09-19 60 views
0

我想了解如何左對齊UITableView中的條目。我正在查看第8章Wei-Meng Lee的書「Beginning iOS 4 Application Development(WROX)」的源代碼。UITableView - 如何左對齊?

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的文章,我只能在編輯時找到左對齊的設置。我如何設置它只是查看和滾動?

感謝您的幫助......

回答

3

刪除此:

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

此委託方法被調用的每一行,這裏的代碼返回的隔日1行的值。

+0

謝謝尼克......它像一個魅力。我完全錯過了。我會去閱讀「indentationLevelForRowAtIndexPath」 – padapa

+0

我很高興能夠提供幫助。我甚至沒有意識到這是一種可用的委託方法,直到我在樣本中看到它。 – Nick