注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

iphone SDK实现tableviewcell  

2013-05-08 21:45:04|  分类: iphone sdk |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
用iphone sdk实现tableviewcell,代码如下:
Utility.m

UIImage *stringImage(NSString *string, UIFont *aFont, CGFloat inset)

{

    CGSize baseSize = [string sizeWithFont:aFont];

    CGSize adjustedSize = CGSizeMake(baseSize.width + inset * 2, baseSize.height + inset * 2);

    

UIGraphicsBeginImageContext(adjustedSize);

CGContextRef context = UIGraphicsGetCurrentContext();

    

    // Draw white backdrop

    CGRect bounds = (CGRect){.size = adjustedSize};

[[UIColor whiteColor] set];

CGContextAddRect(context, bounds);

CGContextFillPath(context);

    

    // Draw a black edge

    [[UIColor blackColor] set];

CGContextAddRect(context, bounds);

    CGContextSetLineWidth(context, inset);

    CGContextStrokePath(context);


    // Draw the string in black

    CGRect insetBounds = CGRectInset(bounds, inset, inset);

    [string drawInRect:insetBounds withFont:aFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];

    

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}


----------------------------------------------
main.m

#import <UIKit/UIKit.h>

#import "Utility.h"


@interface TestBedViewController : UITableViewController

@end


@implementation TestBedViewController


// Number of sections

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView

{

return 1;

}


// Rows per section

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section

{

    return 4;

}


// Return a cell for the index path

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell = [cell initWithStyle:indexPath.row reuseIdentifier:@"cell"];

    cell.textLabel.text = @[@"UITableViewCellStyleDefault", @"UITableViewCellStyleValue1", @"UITableViewCellStyleValue2", @"UITableViewCellStyleSubtitle"][indexPath.row];

    cell.detailTextLabel.text = @"Detail Label";

    cell.imageView.image = stringImage(@"[*]", [UIFont fontWithName:@"Futura" size:18.0f], 6.0f);


return cell;

}


// On selection, update the title and enable find/deselect

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    self.title = cell.textLabel.text;

    self.navigationItem.rightBarButtonItem.enabled = YES;

    self.navigationItem.leftBarButtonItem.enabled = YES;

}


// Deselect any current selection

- (void) deselect

{

    NSArray *paths = [self.tableView indexPathsForSelectedRows];

    if (!paths.count) return;

    

    NSIndexPath *path = paths[0];

    [self.tableView deselectRowAtIndexPath:path animated:YES];

    self.navigationItem.rightBarButtonItem.enabled = NO;

    self.navigationItem.leftBarButtonItem.enabled = NO;

    

    self.title = nil;

}


// Move to the selection

- (void) find

{

    [self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];

}



// Set up table

- (void) loadView

{

    [super loadView];

    

    self.navigationItem.rightBarButtonItem = BARBUTTON(@"Deselect", @selector(deselect));

    self.navigationItem.leftBarButtonItem = BARBUTTON(@"Find", @selector(find));

    self.navigationItem.rightBarButtonItem.enabled = NO;

    self.navigationItem.leftBarButtonItem.enabled = NO;

    

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

@end


#pragma mark -


#pragma mark Application Setup

@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>

{

UIWindow *window;

}

@end

@implementation TestBedAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

{

    // [application setStatusBarHidden:YES];

    [[UINavigationBar appearance] setTintColor:COOKBOOK_PURPLE_COLOR];

    

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

TestBedViewController *tbvc = [[TestBedViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tbvc];

    window.rootViewController = nav;

[window makeKeyAndVisible];

    return YES;

}

@end

int main(int argc, char *argv[]) {

    @autoreleasepool {

        int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");

        return retVal;

    }

}

  评论这张
 
阅读(44)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017