I am trying to create a (not very) custom UITableView with a button on the left side of the cell, and text to the right of the button.
I try to calculate where to put the UILabel that is part of the cell but changing the frame has no effect (and it doesn't even appear to be computed -- all zeros).
My question is: when is the frame size for the label computed? (So that changes will have an effect).
Is this the right way to do it?
Code snippets below.
First is from the cell init (called in response to the dequeueReusable...
and yes, 'buttonColor', 'onTapSel', 'buttonFrame', and 'buttonColor' are "member variables (File static globals)
- (id)initWithStyle: (UITableViewCellStyle)style
reuseIdentifier: (NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (nil == buttonColor)
{ buttonColor = kDefaultCellButtonColor; }
if (nil == onTapSel)
{ onTapSel = #selector(defaultCellButtonTapped:); }
if (self)
{
EGCellButton * cellButton = (EGCellButton *)[[EGCellButton alloc ] initWithFrame:buttonFrame ];
cellButton.backgroundColor = buttonColor;
[cellButton setTitle:buttonLabel forState:UIControlStateNormal];
[cellButton addTarget:self action:(onTapSel) forControlEvents: UIControlEventTouchUpInside];
[self setCellButton:cellButton];
float xx = buttonFrame.origin.x + buttonFrame.size.width + 5;
CGRect frame = self.textLabel.frame;
frame.origin.x += xx;
self.textLabel.frame = frame;
[self addSubview:cellButton];
}
return self;
}
and now from the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EGButtonTableCellID";
EGButtonTableCell * cell = (EGButtonTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.cellButton.indexPath = indexPath;
// Configure the cell...
cell.textLabel.text = #"abcdefghijklmnopqrstuvwxyz";
float xx = cell.cellButton.frame.origin.x + cell.cellButton.frame.size.width + 5;
CGRect frame = cell.textLabel.frame;
frame.origin.x += xx;
cell.textLabel.frame = frame;
return cell;
}
The result being that the button appears on the left side of the cell (as intended), but the first letter I see in the label is 'g', so the first 6 chars are hidden behind the button (not intended).
When I dump the values in the frame, all except origin.x are zero in both methods.
This should work, yes? no? why not? etc.
Thank you all very much!
:bp:
Set frame for your UILabel and UIButton in layoutSubviews method of your custom UITableViewCell
See Apple Documentation on the subject:
Subclasses can override this method as needed to perform more precise
layout of their subviews. You should override this method only if the
autoresizing and constraint-based behaviors of the subviews do not
offer the behavior you want. You can use your implementation to set
the frame rectangles of your subviews directly.
You can create a custom UITableViewCell, access objects in the class or implement methods.
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#property (nonatomic, retain) UILabel *customCellLabel;
#property (nonatomic, retain) UIButton *customCellButton;
#end
CustomCell.m
#import "TLCell.h"
#implementation CustomCell
#synthesize customCellLabel, customCellButton
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
customCellLabel = [[UILabel alloc] init];
customCellLabel.frame = CGRectMake(180, 0, 60, 30);
[self addSubview:customCellLabel];
customCellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
customCellButton.frame = CGRectMake(0, 0, 60, 30);
[self addSubview:customCellButton];
}
return self;
}
#end
CustomTableView.m
/* --- */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.customLabel setText:#"hello"];
// [cell.
[cell.customButton addTarget:self action:#selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
}
/* --- */
Related
I am trying to create a Card Based News feed (Similar to Facebook app) in iOS.I'm implementing programmatically because the height of UITablecell has to increase/decrease based on the data.
Consider i have one UIlabel and one UIImageView added, then the height of UITablecell should be adjusted based on the fields. If one more text feild is added then height has to increase and if imageview is removed then height has to decrease.
The issue here is I couldn't to add CustomView on the UItablecell programmatically but when i create using Interface builder then its working fine but the height remains constant which i don't want to have.
Can some one please help me out.
TableViewCell.h
#interface TableViewCell : UITableViewCell
#property (strong,nonatomic) UIView *customView;
#property (strong,nonatomic) UILabel *customLabel;
#property (strong,nonatomic) UIImageView *customImage;
#end
TableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// configure control(s)
[self addSubview:self.customView];
[self.customView addSubview:self.customLabel];
[self.customView addSubview:self.customImage];
}
return self;
}
- (UIView *)customView
{
if (!_customView)
{
_customView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.contentView.frame.size.width, 50)];
[_customView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return _customView;
}
- (UIImageView *)customImage
{
if (!_customImage)
{
_customImage = [UIImageView new];
_customImage.clipsToBounds = YES;
[_customImage.layer setBorderColor: [[UIColor grayColor] CGColor]];
[_customImage.layer setBorderWidth: 1.0];
_customImage.contentMode = UIViewContentModeCenter;
[_customImage setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return _customImage;
}
- (UILabel *)customLabel
{
if (!_customLabel)
{
_customLabel = [UILabel new];
[_customLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return _customLabel;
}
UITableVIewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!_stubCell)
{
_stubCell = [tableView dequeueReusableCellWithIdentifier:#"TableCell"];
}
});
CGFloat height = [_stubCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height + 1;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.f;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TableCell";
// Similar to UITableViewCell, but
_stubCell = (TableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (_stubCell == nil) {
_stubCell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Just want to test, so I hardcode the data
_stubCell.customLabel.text = #"Testing";
return cell;
}
I think it may be better to create multiple nibs for the same class UITableViewCell, one for each of the possible layouts that you need. Depending on which nib is used then set heightForRowAtIndexPath to the height of the nib.
This will give you predictability of the layout for each situation and you connect only the fields defined in UITableViewCell in cellForRowAtIndexPath.
never mind i found answer by myself. I am calculating the height of each UIView that is added the super view. Based on the total height the UITableview height increases and decrease and also using auto layout adjusting the layout constraints made me simple.
I am still working on the design once its done i'll post the code so that it will be help ful for other developers.
I've got a table view where the cells are created as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"TempTitle";
cell.textLabel.font = [UIFont fontWithName: #"AppleSDGothicNeo-Bold" size:16.0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *numberLabel = [[UILabel alloc] init];
[numberLabel setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
[numberLabel setText:[NSString stringWithFormat:#"%#", [self.arrayOne objectAtIndex:indexPath.row]]];
UIButton *buttonDown = [[UIButton alloc] init];
[buttonDown setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
buttonDown.tag = indexPath.row;
[buttonDown addTarget:self action:#selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonUp = [[UIButton alloc] init];
[buttonUp setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
[cell addSubview:buttonDown];
[cell addSubview:numberLabel];
[cell addSubview:buttonUp];
return cell;
}
where self.arrayOne (name changed for this thread) holds integer values that are displayed in each cell. The method when buttonDown is selected is as following:
- (void) quantityDown:(id)sender {
int clicked = ((UIButton *)sender).tag;
... math to lower int by one and save the new value in arrayOne
... this part works just fine. The value does go down, as intended.
[self.tableView reloadData];
}
When the tableView reloads, a new label is printed on top of the existing label in that cell, as can be seen in the image below:
I can only assume that new buttons are being printed on top of the existing ones as well. This is both expensive and makes the numbers unreadable (especially if you change it multiple times!). Leaving the view and coming back to it shows the new numbers cleanly, but only until you start changing them again.
A similar effect happens when I use the UISegmentedControl at the top. Selecting one or the other changes the contents of the table and runs [self.tableView reloadData]. The textLabel for each cell reloads just fine when this method is called, but the sub views do not reload, and instead stack upon one another.
How would I go about writing this so that there is only ever one subview in each cell, instead of multiple stacked upon one another? Something speedy and not expensive on resources. Maybe removing all subviews and then adding the new ones? I tried something like
[[cell.contentView viewWithTag:tagVariable]removeFromSuperview];
to no avail. Really, I only need to modify the one cell in the table that the user is clicking in. Except for when the user uses the UISegmentedControl at the top, then all the cells need to be modified.
Thanks in advance.
EDIT 1:
Created a custom UITableViewCell class...
#interface SSCustomTableViewCell : UITableViewCell
#property (nonatomic) UILabel *quantity;
#property (nonatomic) UIButton *down;
#property (nonatomic) UIButton *up;
#end
#implementation SSWeightsTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.quantity = [UILabel new];
[self.contentView addSubview:self.quantity];
self.down = [UIButton new];
[self.contentView addSubview:self.down];
self.up = [UIButton new];
[self.contentView addSubview:self.up];
}
return self;
}
- (void) layoutSubviews {
[super layoutSubviews];
[self.down setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
[self.down setBackgroundColor:[UIColor purpleColor]];
[self.up setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
[self.up setBackgroundColor:[UIColor purpleColor]];
[self.quantity setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
[self.quantity setTintColor:[UIColor purpleColor]];
}
#end
and then in my UITableView class...
#import "SSCustomTableViewCell.h"
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView
registerClass:[SSCustomTableViewCell class]
forCellReuseIdentifier:NSStringFromClass([SSCustomTableViewCell class])
];
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
forIndexPath:indexPath
];
}
but now all I see is the following...
where the subviews are not correctly laid out, nor are they the correct size. Also, within the cellForRowAtIndexPath method, I cannot access the cell's components. When I enter
cell.quantity
I get an error saying that "Property 'quantity' not found on object of type UITableViewCell*"
So I tried
SSCustomTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
forIndexPath:indexPath
];
and the error goes away, but it still looks the same when I run it.
Edit 2: Working Solution
All I had to do was add
cell = [[UITableViewCell alloc]init];
within the cellForRowAtIndexPath method. Everything else stayed the same, and no custom classes required.
You are seeing this error because cell instances are reused. If you add a view each time you configure the cell, you will be adding to cells that already have the subviews on them. You should create your own subclass of UITableViewCell and add the subviews to it in init. Then your method above would just set the values on the various subviews.
Your cell would look something like this:
#interface MyCustomCell : UITableViewCell
#property (nonatomic, strong) UILabel *myCustomLabel;
#end
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.myCustomLabel = [UILabel new];
[self.contentView addSubview:self.myCustomLabel];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Arrange self.myCustomLabel how you want
}
#end
Then your view controller would look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView
registerClass:[MyCustomCell class]
forCellReuseIdentifier:NSStringFromClass([MyCustomCell class])
];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:NSStringFromClass([MyCustomCell class])
forIndexPath:indexPath
];
cell.myCustomLabel.text = #"blah";
return cell;
}
Try this in cell configure method, this will make sure objects doesn't overlap
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
return cell;
}
Hope this will help
I'm creating the cells of a UITableViewController.
In one of them, a small image is contained. I tried the following to make its corners rounded:
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
In another cell prototype, I tried to make the corners of a button rounded:
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
In both cases I included
#import <QuartzCore/QuartzCore.h>
The button and the image whose corners must be rounded are property of the UITableViewController owning them:
#import <UIKit/UIKit.h>
#interface profileRegistrationCellVC : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *profileImage;
#property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll;
#property (weak, nonatomic) IBOutlet UIButton *shootImage;
#end
In relative .m class:
#import "profileRegistrationCellVC.h"
#import <QuartzCore/QuartzCore.h>
#implementation profileRegistrationCellVC
#synthesize profileImage;
#synthesize chooseImageFromRoll;
#synthesize shootImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
shootImage.layer.cornerRadius = 8;
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
profileImage.layer.borderColor = [UIColor whiteColor].CGColor;
profileImage.layer.borderWidth = 20.0;
[self addSubview:profileImage];
[self addSubview:chooseImageFromRoll];
[self addSubview:shootImage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Here's the code of my function cellForRowAtIndexPath, in my uitableviewcontroller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"profileRegistrationCell";
profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.profileImage.image.layer.masksToBounds = YES;
//cell.profileImage.layer.cornerRadius = 5.0;
return cell;
}
else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row];
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Email*"])
cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress;
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Età "]) {
cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return cell;
}
else{
static NSString *CellIdentifier = #"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1];
cell.orientationLabel.text = #"Non specificato";
return cell;
}
}
But in no case I managed to make corners rounded. Can you tell where I'm mistaking?
Thanks
The code snippets above are both correct and have no issues. My assumption is that your issue lies elsewhere in creating the button that contains the image. The following code will create a button and will round it's corners, also I added the border to it in case you want to add that too. You can plug in the image you have in there as well. I have added a image of what this code will create for your reffrence also. Hope it will help you out.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
Below is the image of the button that is related with the above code
Edit:
another way of doing the round corners is to use the method masksToBounds here is an example of it within the generic cell right out of the box from the template.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
cell.imageView.image = [UIImage imageNamed:#"acolon.png"];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
return cell;
}
here is the screen shot of the result:
i know you using a custom cell, so implement the maskToBount in the cellForRowAtImdexPath or wherever you are populating the tableview with its custom cells.
I am wondering what is the best way to make a UITextField fit a UITableView cell.
I have used this method before:
#implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 0, bounds.origin.y + 10,
bounds.size.width - 30, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
#end
But this causes an issue:
Category is implementing a method which will also be implemented by its primary class
Although I have seen ways to hide these warnings, it feels like this is more of a hack than a correct method.
What is the best way to fit a UItextField to a cell. This is my field:
// Username field
usernameField = [[UITextField alloc] initWithFrame:(CGRectMake(10, 0, 300, 43))];
usernameField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
It is input into the cell like this:
if (indexPath.row == 0) {
[cell.contentView addSubview:usernameField];
}
Image:
I'd suggest doing this by overriding your custom UITableViewCell's layoutSubviews method. Much simpler there imo. Something like:
- (void)layoutSubviews
{
[super layoutSubviews];
float w, h;
w = (int)(self.frame.size.width - 30);
h = (int)(self.frame.size.height - 16);
[self.detailTextLabel setFrame:CGRectMake(0, 10, w, h)];
}
Here's a fragment of the custom cell initialization
#interface MyCustomCell: UITableViewCell {
}
#end
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}
Here's a fragment of where the custom cell would be created:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
I know how to create a tableview with a single column and multiple rows, but I don't know how to create a tableview with multiple rows and multiple columns.
Can anybody help me?
This is how i did it:
#import <Foundation/Foundation.h>
#interface MyTableCell : UITableViewCell
{
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
#end
implementation:
#import "MyTableCell.h"
#define LINE_WIDTH 0.25
#implementation MyTableCell
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)addColumn:(CGFloat)position
{
[columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, LINE_WIDTH);
for (int i = 0; i < [columns count]; i++)
{
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
#end
and the last piece, cellForRowAtIndexPath
MyTableCell *cell = (MyTableCell *)[rankingTableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// load cell from nib to controller's IBOutlet
[[NSBundle mainBundle] loadNibNamed:#"MyTableCellView" owner:self options:nil];
// assign IBOutlet to cell
cell = myCell;
self.myCell = nil;
}
id modelObject = [myModel objectAtIndex:[indexPath.row]];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [modelObject firstField];
label = (UILabel *)[cell viewWithTag:2];
label.text = [modelObject secondField];
label = (UILabel *)[cell viewWithTag:3];
label.text = [modelObject thirdField];
return cell;
}
i think this code will help you UITableView isn't really designed for multiple columns. But you can simulate columns by creating a custom UITableCell class. Build your custom cell in Interface Builder, adding elements for each column. Give each element a tag so you can reference it in your controller.
Give your controller an outlet to load the cell from your nib:
#property(nonatomic,retain)IBOutlet UITableViewCell *myCell;
Then, in your table view delegate's cellForRowAtIndexPath method, assign those values by tag.