开发者

UITableView - How to left justify?

开发者 https://www.devze.com 2023-04-05 23:14 出处:网络
I am trying to understand how to left justify the entries in a UITableView.I was looking at the source code from chapter 8 Wei-Meng Lee\'s book \"Beginning iOS 4 Application Development (WROX)\".

I am trying to understand how to left justify the entries in a UITableView. I was looking at the source code from chapter 8 Wei-Meng Lee's book "Beginning iOS 4 Application Development (WROX)".

UITableView - How to left justify?

The Source 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

I have been trying (Without Success) to change the UITableView so the entries are left justified. I have read a lot of posts about UITableViews and I can only find settings for left justified when editing. How do I set it for just viewing and scrolling?

Thanks for the help...


Remove this:

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

This delegate method gets called for every row and the code here is returning a value of 1 every other row.

0

精彩评论

暂无评论...
验证码 换一张
取 消