Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PrettyTableViewCell sample
//
// SettingController.m
// Sharing
//
// Created by jonnyzheng on 8/18/13.
// Copyright (c) 2013 jonny. All rights reserved.
//
#import "SettingController.h"
#import "PrettyTableViewCell.h"
@interface SettingController ()
@end
@implementation SettingController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView setBackgroundView:nil];
self.tableView.backgroundColor = [UIColor colorWithRed:248.0/255.0
green:244.0/255.0
blue:239.0/255.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return 1;
}
if (section == 1) {
return 2;
}
if (section == 2) {
return 4;
}
if (section == 3) {
return 1;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PrettyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
[self prepareNameCell:cell];
break;
}
break;
default:
break;
}
//设置圆角弧度和阴影
cell.cornerRadius = 5;
cell.shadowOpacity = 0.07;
cell.backgroundColor = [UIColor whiteColor];
//设置选择时候的颜色
cell.selectionGradientStartColor = [UIColor colorWithRed:248.0/255.0
green:244.0/255.0
blue:239.0/255.0 alpha:1];
cell.SelectionGradientEndColor = [UIColor colorWithRed:248.0/255.0
green:244.0/255.0
blue:239.0/255.0 alpha:1];
// Configure the cell...
[cell prepareForTableView:tableView indexPath:indexPath];
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
-(void) prepareNameCell:(PrettyTableViewCell *)cell
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, 15.0)];
nameLable.text = @"用户名:";
nameLable.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0];
nameLable.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addSubview:nameLable];
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 100.0, 15.0)];
name.text = @"jonny";
name.translatesAutoresizingMaskIntoConstraints = NO;
name.font = [UIFont fontWithName:@"Helvetica" size:15.0];
[cell.contentView addSubview:name];
NSLayoutConstraint *constraintXName = [NSLayoutConstraint constraintWithItem:name
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: nameLable
attribute:NSLayoutAttributeTrailing
multiplier:1.0 constant:5.0];
NSLayoutConstraint *constraintYName = [NSLayoutConstraint constraintWithItem:name
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem: cell.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0];
NSLayoutConstraint *constraintX = [NSLayoutConstraint constraintWithItem:nameLable
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: cell.contentView
attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:10.0f];
NSLayoutConstraint *constraintY = [NSLayoutConstraint constraintWithItem:nameLable
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem: cell.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0];
[cell.contentView addConstraints: [[NSArray alloc] initWithObjects:
constraintXName,constraintYName,constraintX,constraintY,nil]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment