UITableView - 如何左对齐?

时间:2011-09-19 02:02:20

标签: iphone ios uitableview

我试图了解如何在UITableView中左对齐条目。我正在查看第8章Wei-Meng Lee的书“开始iOS 4应用程序开发(WROX)”中的源代码。

The running app looks like this

Source看起来像这样:

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 个答案:

答案 0 :(得分:3)

删除它:

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

为每一行调用此委托方法,此处的代码每隔一行返回一个值。