看QQ分享,发现一个不错的自定义cell方法,如下:
xiaolongzhang 张小龙?😳
//
// cellInfo.h
// sdkDemo
//
// Created by <mark>xiaolongzhang</mark> on 13-7-3.
// Copyright (c) 2013年 xiaolongzhang. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef enum
{
kApiQZone,
kApiQQVip,
kApiQQT,
kApiQQ,
kApiQuick,
}apiType;
@interface cellInfo : NSObject
+ (cellInfo *)info:(NSString *)title target:(id)target Sel:(SEL)sel viewController:(UIViewController *)viewController userInfo:(id)userInfo;
+ (cellInfo *)info:(NSString *)title target:(id)target Sel:(SEL)sel viewController:(UIViewController *)viewController;
@property (nonatomic, retain)NSString *title;
@property (nonatomic, assign)id target;
@property (nonatomic, assign)SEL sel;
@property (nonatomic, retain)UIViewController *viewController;
@property (nonatomic, retain)id userInfo;
@end
//
// cellInfo.m
// sdkDemo
//
// Created by xiaolongzhang on 13-7-3.
// Copyright (c) 2013年 xiaolongzhang. All rights reserved.
//
#import "cellInfo.h"
@implementation cellInfo
+ (cellInfo *)info:(NSString *)title target:(id)target Sel:(SEL)sel viewController:(id)viewController
{
cellInfo *info = [[cellInfo alloc] init];
info.title = title;
info.target = target;
info.sel = sel;
info.viewController = viewController;
info.userInfo = nil;
return info;
}
+ (cellInfo *)info:(NSString *)title target:(id)target Sel:(SEL)sel viewController:(UIViewController *)viewController userInfo:(NSDictionary *)userInfo
{
cellInfo *info = [[cellInfo alloc] init];
info.title = title;
info.target = target;
info.sel = sel;
info.viewController = viewController;
info.userInfo = userInfo;
return info;
}
@synthesize title = _title;
@synthesize target = _target;
@synthesize sel = _sel;
@synthesize viewController = _viewController;
@synthesize userInfo = _userInfo;
@end
@interface sdkDemoViewController ()
{
FGalleryViewController *_localGallery;
NSInteger _currentTableViewTag;
}
@property (nonatomic, retain)NSMutableArray *sectionName;
@property (nonatomic, retain)NSMutableArray *sectionRow;
@end
@implementation sdkDemoViewController
@synthesize sectionName = _sectionName;
@synthesize sectionRow = _sectionRow;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
// Custom initialization
self.sectionName = [NSMutableArray arrayWithCapacity:1];
self.sectionRow = [NSMutableArray arrayWithCapacity:1];
[self loadData];
_isLogined = NO;
_currentTableViewTag = 0;
}
return self;
}
- (void)loadData
{
NSMutableArray *cellLogin = [NSMutableArray arrayWithCapacity:1];
[cellLogin addObject:[cellInfo info:@"第三方登录" target:self Sel:@selector(login) viewController:nil]];
[[self sectionName] addObject:@"登录"];
[[self sectionRow] addObject:cellLogin];
NSMutableArray *cellApiInfo = [NSMutableArray arrayWithCapacity:4];
[cellApiInfo addObject:[cellInfo info:@"QQ定向分享" target:nil Sel:@selector(pushSelectViewController:) viewController:nil userInfo:[NSNumber numberWithInteger:kApiQQ]]];
[cellApiInfo addObject:[cellInfo info:@"QQ空间" target:self Sel:@selector(pushSelectViewController:) viewController:nil userInfo:[NSNumber numberWithInteger:kApiQZone]]];
[cellApiInfo addObject:[cellInfo info:@"QQ会员" target:nil Sel:@selector(pushSelectViewController:) viewController:nil userInfo:[NSNumber numberWithInteger:kApiQQVip]]];
[[self sectionName] addObject:@"api"];
[[self sectionRow] addObject:cellApiInfo];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)login
{
NSArray* permissions = [NSArray arrayWithObjects:
kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
kOPEN_PERMISSION_ADD_ALBUM,
kOPEN_PERMISSION_ADD_ONE_BLOG,
kOPEN_PERMISSION_ADD_SHARE,
kOPEN_PERMISSION_ADD_TOPIC,
kOPEN_PERMISSION_CHECK_PAGE_FANS,
kOPEN_PERMISSION_GET_INFO,
kOPEN_PERMISSION_GET_OTHER_INFO,
kOPEN_PERMISSION_LIST_ALBUM,
kOPEN_PERMISSION_UPLOAD_PIC,
kOPEN_PERMISSION_GET_VIP_INFO,
kOPEN_PERMISSION_GET_VIP_RICH_INFO,
nil];
[[[sdkCall getinstance] oauth] authorize:permissions inSafari:NO];
}
- (void)pushSelectViewController:(NSNumber *)apiType
{
UIViewController *rootViewController = nil;
switch ([apiType unsignedIntegerValue]) {
case kApiQZone:
rootViewController = [[QZoneTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
break;
case kApiQQVip:
rootViewController = [[QQVipTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
break;
case kApiQQ:
rootViewController = [[QQApiDemoController alloc] init];
break;
default:
break;
}
[[self navigationController] pushViewController:rootViewController animated:YES];
__RELEASE(rootViewController);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [[self sectionName] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[[self sectionRow] objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:CellIdentifier];
}
NSString *title = nil;
NSMutableArray *array = [[self sectionRow] objectAtIndex:section];
if ([array isKindOfClass:[NSMutableArray class]])
{
title = [[array objectAtIndex:row] title];
}
if (nil == title)
{
title = @"未知";
}
[[cell textLabel] setText:title];
[[cell textLabel] setTextAlignment:NSTextAlignmentCenter];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// if (0 == indexPath.row
// || 1 == indexPath.row)
// {
// [[cell textLabel] setEnabled:YES];
// }
// Configure the cell...
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[self sectionName] objectAtIndex:section];
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger row = [indexPath row];
NSInteger section = [indexPath section];
NSArray *array = [[self sectionRow] objectAtIndex:section];
if ([array isKindOfClass:[NSMutableArray class]])
{
cellInfo *info = (cellInfo *)[array objectAtIndex:row];
if ([self respondsToSelector:[info sel]])
{
if (nil == [info userInfo])
{
[self performSelector:[info sel]];
}
else
{
[self performSelector:[info sel] withObject:[info userInfo]];
}
}
}
[[tableView cellForRowAtIndexPath:indexPath] setHighlighted:NO animated:NO];
}
@end