Bonfire DA

Bonfire Development Advisors, Inc. Software design and development.

Categories

  • Apps
  • Code Snippets
  • Persona Archive

Publications

  • The Power of the Persona: A Case Study

About





































































iPhone & iPad: Coding a "Done" button in UITableViewCell

Here's code for making a UITableViewCell within a grouped UITableView look and function as an iphone button.


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {/* other stuff */

static NSString *DoneButtonCellIdentifier = @"DoneButtonCell";

 

cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoneButtonCellIdentifier];

 

if (cell == nil) {

// Create a cell to display an emotion.

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DoneButtonCellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryNone;

cell.selectionStyle=UITableViewCellSelectionStyleNone;

} else {

if ([cell.contentView subviews]){

for (UIView *subview in [cell.contentView subviews]) {

[subview removeFromSuperview];

}

}

}

 

//creates the button look

int pos=10;

int width=280;

if (!portraitOrientation)

{

pos=55;

width=345;

}

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(pos, 0, width, 43)] autorelease];

[view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];

CAGradientLayer *gradient = [CAGradientLayer layer];

[gradient setCornerRadius:12.0f];

[gradient setMasksToBounds:YES];

[gradient setBorderWidth:1.6f];

[gradient setBorderColor:[[UIColor darkGrayColor] CGColor]];

gradient.frame = view.bounds;

gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor greenColor] CGColor], (id)[[UIColor colorWithRed:.05 green:.65 blue:.05 alpha:1] CGColor],(id)[[UIColor colorWithRed:.05 green:.50 blue:.05 alpha:1] CGColor], (id)[[UIColor colorWithRed:.05 green:.55 blue:.05 alpha:1] CGColor], (id)[[UIColor colorWithRed:.05 green:.65 blue:.05 alpha:1] CGColor], nil];

gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.10],[NSNumber numberWithFloat:0.49],[NSNumber numberWithFloat:0.51],[NSNumber numberWithFloat:0.74],[NSNumber numberWithFloat:1.0], nil];

 

[view.layer insertSublayer:gradient atIndex:0];

[cell.contentView addSubview:view];

 

//"button" label

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(pos, 0, width, 37)] autorelease];

[label setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];

label.textAlignment = UITextAlignmentCenter;

label.font = [UIFont boldSystemFontOfSize:23.0];

label.textAlignment = UITextAlignmentCenter;

label.textColor = [UIColor whiteColor];

label.shadowColor = [UIColor lightGrayColor];

label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;

label.backgroundColor = [UIColor clearColor];

label.text=@"Done";

[cell.contentView addSubview: label];

 

//makes cell transparent

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

[backView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)];

backView.backgroundColor = [UIColor clearColor];

cell.backgroundView = backView;

 

return cell;

}