UITableView的常用方法与示例

实例方法


dequeueReusableCellWithIdentifier:
初始化一个指定重用标识符的UITableCell对象

两个协议


UITableViewDataSource

tableView:numberOfRowsInSection: (required)
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑

UITableViewDelegate

tableView:didSelectRowAtIndexPath:
点击cell时执行的委托方法


简单的Demo


效果图



点击细节按钮将显示一个AlertView。如果想显示细节信息,亦可以用Master-Detail模板进行创建项目,里面提供好了很多现成的方法,更加方便和快捷.

初始准备

实现协议,增加成员
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_redFlowers;
    NSArray *_blueFlowers;
}

预定义数字
#define kSectionCount 2
#define kRedSection 0
#define kBlueSection 1

初始化成员
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _redFlowers = @[@"Gerbera", @"Peony", @"Rose", @"poppy"];
    _blueFlowers = @[@"Hyacinth", @"Hydrangea", @"Sea Holly", @"Phlox", @"Iris"];
}


实现委托方法

分区及分区标题,分区中的行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return kSectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case kRedSection:
            return [_redFlowers count];
        case kBlueSection:
            return [_blueFlowers count];
        default:
            return 0;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case kRedSection:
            return @"Red";
        case kBlueSection:
            return @"Blue";
        default:
            return @"Unknown";
    }
}


显示的Cell信息

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"];
    
    switch (indexPath.section) {
        case kRedSection:
            cell.textLabel.text = _redFlowers[indexPath.row];
            break;
        case kBlueSection:
            cell.textLabel.text = _blueFlowers[indexPath.row];
            break;
        default:
            cell.textLabel.text = @"Unknown";
    }
    
    UIImage *flowerImage;
    flowerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", cell.textLabel.text, @".png"]];
    cell.imageView.image = flowerImage;
    
    return cell;
}

这里重用标识符可以是自定义的Cell,也可以是在xib中设置好的系统带的样式,然后在属性标识板中给它设置标识符。
图片是自己个人加入到项目中的。

然后实现选择时弹出AlertView的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *showSelection;
    NSString *messageString;
    
    switch (indexPath.section) {
        case kRedSection:
            messageString = [NSString stringWithFormat:@"You chose the red flower - %@", _redFlowers[indexPath.row]];
            break;
        case kBlueSection:
            messageString = [NSString stringWithFormat:@"You chose the blue flower - %@", _blueFlowers[indexPath.row]];
            break;
        default:
            messageString = @"Unknown";
            break;
    }
    
    showSelection = [[UIAlertView alloc] initWithTitle:@"Flower Selected"
                                               message:messageString
                                              delegate:nil
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles: nil];
    [showSelection show];
}


 

posted @ 2013-07-19 18:24  坚固66  阅读(195)  评论(0编辑  收藏  举报