Programiticaly Multiple Columns in Tableview ios - ios

I am trying to create a table with multiple columns, I am using array of cells.
Following is my code, I get single columns every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
CGFloat x = 0.0f;
UIView *lastCol = [[cell columnCells] lastObject];
if (lastCol) x = lastCol.frame.origin.x + lastCol.frame.size.width;
for (int i = 0; i < 2; i++) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, i, 40.0f)] ;
UIView *gridCell = l;
CGRect f = gridCell.frame;
f.origin.x += x;
gridCell.frame = f;
[cell.contentView addSubview:gridCell];
CGFloat colWidth = [self widthForColumn:i];
x += colWidth + 1.0f;
[[cell columnCells] addObject:gridCell];
}
for (int i = 0; i < 2; i++) {
UILabel *l = (UILabel*)[cell columnCells][i];
l.text =self.department[indexPath.row];
}
return cell;
}

How about this?
// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
if (i==0) {
mView.backgroundColor = [UIColor redColor];
} else if (i==1) {
mView.backgroundColor = [UIColor greenColor];
} else if (i==2) {
mView.backgroundColor = [UIColor blueColor];
}
[cell addSubview:mView];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Thanks to Fahim, I just modified his code and got what I wanted.
Here's the exact requirement which I was looking for.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];
[label setText:self.department[indexPath.row]];
[cell addSubview:mView];
[mView addSubview:label];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Related

UITableView not display text on devices

I develop application contains UITableView (not Controller) and it was added programmatically. I didn't use any nib file. and found out that in simulator work fine but's when I build on device Table shown up but didn't see any text on it.
Datasource and delegate is already connected.
here some code
if (kPortrait) {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(568, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
} else {
optionTV = [[UITableView alloc] initWithFrame:CGRectMake(824, 75, 200, 44 * [optionArray count]) style:UITableViewStylePlain];
}
[optionTV setAutoresizesSubviews:YES];
[optionTV setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[optionTV setDataSource:self];
[optionTV setDelegate:self];
optionTV.alpha = 0.0f;
optionTV.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.50];
[self.view addSubview:optionTV];
[UIView animateWithDuration:0.5f animations:^{
trafficBT.transform = CGAffineTransformMakeTranslation(-200, 0);
optionTV.alpha = 0.8;
}];
for delegate method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"tableView Data num row: %d", [optionArray count]);
return [optionArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
if (kIsIPhone) {
cell.textLabel.font = [UIFont systemFontOfSize:18];
} else {
cell.textLabel.font = [UIFont systemFontOfSize:20];
}
cell.textLabel.text = [optionArray objectAtIndex:indexPath.row];
return cell;
}

UITableView dequeue not working as expected

As I scroll down the list, all the rows are there, but they keep adding more subviews the more they appear on the visible frame
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
}
NSUInteger row = indexPath.row;
[self createCell: cell onRow: row];
return cell;
}
- (void) createCell: (UITableViewCell*)cell onRow: (NSUInteger)row
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I think you need to execute almost all of the logic that's in createCell: only within the if (cell == nil){ segment of your code. The part that should execute where you're currently calling createCell: is just getting a reference to the titleLabel and setting its text value.
To clarify, here's the kind of modification I'm suggesting (not tested, but should give the right idea):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
[self setUpCell: cell];
}
NSUInteger row = indexPath.row;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:42];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
return cell;
}
- (void) setUpCell: (UITableViewCell*)cell
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.tag = 42;
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}

iOS create a invisible space between cells

I have a standard tableview here and I wanted to be able to add a space between each cell that is invisible/hidden that shows the background. What is the correct way of doing it?
Here is the code I am using:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// Format each frame that holds each piece of content in each row
CGRect countyImageFrame = CGRectMake(275, 6, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.tag = 0016;
countyImageLabel.backgroundColor = BG_COLOR
[cell.contentView addSubview:countyImageLabel];
CGRect callTypeImageFrame = CGRectMake(275, 30, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.tag = 0017;
callTypeImageLabel.backgroundColor = BG_COLOR
[cell.contentView addSubview:callTypeImageLabel];
CGRect callTypeFrame = CGRectMake(5, 2, 175, 15);
UILabel *callTypeLabel = [[UILabel alloc] initWithFrame:callTypeFrame];
callTypeLabel.tag = 0011;
callTypeLabel.backgroundColor = BG_COLOR
callTypeLabel.numberOfLines = 2;
callTypeLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:callTypeLabel];
CGRect locationFrame = CGRectMake(5, 21, 175, 10);
UILabel *locationLabel = [[UILabel alloc] initWithFrame:locationFrame];
locationLabel.tag = 0014;
locationLabel.backgroundColor = BG_COLOR
locationLabel.numberOfLines = 2;
locationLabel.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:locationLabel];
CGRect unitsFrame = CGRectMake(3, 40, 175, 10);
UILabel *unitsLabel = [[UILabel alloc] initWithFrame:unitsFrame];
unitsLabel.tag = 0012;
unitsLabel.backgroundColor = BG_COLOR
unitsLabel.textColor = [UIColor blackColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10];
[cell.contentView addSubview:unitsLabel];
CGRect stationFrame = CGRectMake(185 , 28, 85, 20);
UILabel *stationLabel = [[UILabel alloc] initWithFrame:stationFrame];
stationLabel.tag = 0013;
stationLabel.backgroundColor = BG_COLOR
stationLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:stationLabel];
CGRect callnumberFrame = CGRectMake(185 , 5, 80, 20);
UILabel *callnumberLabel = [[UILabel alloc] initWithFrame:callnumberFrame];
callnumberLabel.tag = 0015;
callnumberLabel.backgroundColor = BG_COLOR
callnumberLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:callnumberLabel];
}
// Display content in each cell
if ([currentCall.county isEqualToString:#"W"]) {
UIImage *countyImage = [UIImage imageNamed:#"blue.png"];
UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016];
countyImageLabel.image = countyImage;
}
else {
UIImage *countyImage = [UIImage imageNamed:#"green.png"];
UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016];
countyImageLabel.image = countyImage;
}
if ([currentCall.callType isEqualToString:#"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:#"red.png"];
UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017];
callTypeImageLabel.image = callTypeImage;
}
else {
UIImage *callTypeImage = [UIImage imageNamed:#"yellow.png"];
UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017];
callTypeImageLabel.image = callTypeImage;
}
UILabel *callTypeLabel = (UILabel *)[cell.contentView viewWithTag:0011];
callTypeLabel.text = [currentCall currentCallType];
UILabel *locationLabel = (UILabel *)[cell.contentView viewWithTag:0014];
locationLabel.text = [currentCall location];
UILabel *unitsLabel = (UILabel *)[cell.contentView viewWithTag:0012];
unitsLabel.text = [currentCall units];
NSString *station = [#"Station: " stringByAppendingString:currentCall.station];
UILabel *stationLabel = (UILabel *)[cell.contentView viewWithTag:0013];
stationLabel.text = station;
NSString *callnumber = [#"Call No: " stringByAppendingString:currentCall.callnumber];
UILabel *callnumberLabel = (UILabel *)[cell.contentView viewWithTag:0015];
callnumberLabel.text = callnumber;
cell.backgroundColor = BG_COLOR
return cell;
}
// Set how tall each cell is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
Let me start by saying I did not read all your code. That said I have done exactly this. The first approach is to declare in the heightForCell a larger value for the cell above where the gap should be. The cell.contentView might need to have its clipToBounds set. I believe in this case the cell size should be set by setting the cell.frame.size (the origin is 0,0), and the contentsView and backgroundview properly sized too.
The second way to do it is to insert empty cells - cells with a different reuse identifier, and which are just empty cells with everything set to have a clear color.
Just use the trick to have spaces between each cells..
static NSString *CELL_ID2 = #"SOME_STUPID_ID2";
// even rows will be invisible
if (indexPath.row % 2 == 1)
{
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil)
{
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
}
return cell2;
}
I just use different heights and alternate colours between cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (indexPath.row % 2 == 1)
{
static NSString *CellIdentifier = #"cellID1";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWhite];
return cell;
} else {
static NSString *CellIdentifier2 = #"cellID2";
UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier2];
}
cell2.backgroundColor = [UIColor clearColor];
return cell2;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 1) {
return 40.0;
} else {
return 2.0;
}
}

strike on the uilabel disappears in uitableview did select cell

1)i want to create a striked text so i have used thet ext frame and i have created it and keep on the lbal it works fine when i have used it in the table cell when i am selecting the cell the strike on the label disaperared
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
tableView1.backgroundColor = [UIColor clearColor];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C.png"]];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C_h.png"]];
UILabel *price1_lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,80,100,26)];
price1_lbl.text = #"label";
price1_lbl.textColor = [UIColor grayColor];
price1_lbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:(13.0)];
price1_lbl.backgroundColor = [UIColor clearColor];
NSString *string =price1_lbl.text;
CGSize stringSize = [string sizeWithFont:price1_lbl.font];
CGRect buttonFrame = price1_lbl.frame;
CGRect labelFrame = CGRectMake(buttonFrame.origin.x ,
4+buttonFrame.origin.y + stringSize.height/2,
stringSize.width, 1);
UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
lineLabel.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:price1_lbl];
[cell.contentView addSubview:lineLabel];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
secondviewcontroller *obj=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
}
Try to add the strikethrough line directly to the label with the text (price1_lbl). If that doesn't work you may want to give this lib a try.
UPDATE:
You can use this code:
CGSize size = [price1_lbl.text sizeWithFont:[UIFont systemFontOfSize:16.0f]];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0 , label.frame.size.height / 2, size.width, 1)];
line.backgroundColor = [UIColor blackColor];
[price1_lbl addSubview:line];

titleForHeaderSection from NSTableViewCell

How can I retrieve the current header section name from an NSTableViewCell item?
Currently I have a method called configureCell which determines how to style the custom cell I have. This data comes from a pList file.
-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath{
UILabel *label;
UIView *backView;
// NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section];
NSString *fatPercent = [[self.milkTypes valueForKey:#"Finland"] objectAtIndex:indexPath.row];
label = (UILabel *)[tableViewCell viewWithTag:1];
label.text = #"Fat Percent test";
backView = (UIView *)[tableViewCell viewWithTag:2];
backView.backgroundColor = [self colorWithHexString: fatPercent];
}
Where I've commented out the line for *country I need to retrieve the current section I'm in. Currently it's statically set to Finland which is the array name from the pList.
For a better understanding on how my code is laid out, here is the majority of the Table Controller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.milkTypes count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.milkTypes allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *country = [self tableView:tableView titleForHeaderInSection:section];
return [[self.milkTypes valueForKey:country] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier;
UITableViewCell *cell;
cellIdentifier = [NSString stringWithFormat:#"MilkCell"];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [self tableViewCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}
[self configureCell:cell forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
CGRect rect;
UILabel *label;
UIView *backView;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
float center = (float) (40-20)/2;
rect = CGRectMake(15, center, 270, 20);
label = [[UILabel alloc] initWithFrame:rect];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release];
rect = CGRectMake(280, 10, 20, 40-20);
backView = [[UIView alloc] initWithFrame:rect];
backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
backView.backgroundColor = [UIColor clearColor];
backView.tag = 2;
[cell.contentView addSubview:backView];
[backView release];
return cell;
}
-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath {
UILabel *label;
UIView *backView;
// NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section];
NSString *fatPercent = [[self.milkTypes valueForKey:#"Sweden"] objectAtIndex:indexPath.row];
label = (UILabel *)[tableViewCell viewWithTag:1];
label.text = #"Fat Percent test";
backView = (UIView *)[tableViewCell viewWithTag:2];
backView.backgroundColor = [self colorWithHexString: fatPercent];
}
It seems you already have it anyway, in [[self.milkTypes allKeys] objectAtIndex:section] - just use that instead, so
NSString *country = [[self.milkTypes allKeys] objectAtIndex: indexPath.section];

Resources