How to create multiple column in an one row UITableViewCell in swift? - ios

How to Create multiple column in an one row.
Like this image!. https://pbs.twimg.com/media/CEuO0hUUUAARX_L.png !

You can also use gridview for your requirement like GMGridView or UIGridView
Check below sample code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = #"gridCell";
UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(gridCell == nil)
{
gridCell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier] autorelease];
gridCell.accessoryType = UITableViewCellAccessoryNone;
gridCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSArray* items = [_grids objectAtIndex:indexPath.section];
int imageIndex = 0;
int yOffset = 0;
while (imageIndex < items.count)
{
int yPos = 4 + yOffset * 74;
for(int i = 0; i < 4; ++i)
{
CGRect rect = CGRectMake((18 + i * 80), yPos, 40, 40);
UIImageView* imageView = [self gridImageRect:rect forImage:[items objectAtIndex:imageIndex]];
[gridCell.contentView addSubview:imageView];
[imageView release];
rect = CGRectMake(((80 * i)-4), (yPos+44), 80, 12);
UILabel* label = [self descriptionOfImage:imageIndex inRect:rect];
[gridCell.contentView addSubview:label];
[label release];
++imageIndex;
}
++yOffset;
}
return gridCell;
}

Related

How to populate, Expandable TableView with two NSMutableArray using objective-c

I am using an Expandable UITableview created by Tom Fewster. I want to tweak the example using two NSMutableArrays, which is a scenario whereby if someone wants to populate an expandable/collapse treeview table from webservice json data would want to achieve. So since in his example the GroupCell does not have an array of, I am wondering how can I do it? Please bear in mind that my Objective-C is still rusty hence, I'm asking this question.
With my attempt is only displaying the first ObjectAtIndex:indexPath:0 for the group.
I want to be able to populate the table and get output like this;
Group A
Row 1a
Row 2a
Row 3a
Group B
Row 1b
Row 2b
Group C
Row 1c
Row 2c
Row 3c
and so on.
You may use JSON data as well to explain your answer if you understand it better that way.
Here i want to populate the table with JSON data so the GroupCell show class_name and rowCell show subject_name. This is the console of what I am parsing from the JSON web-service;
(
{
"class_id" = 70;
"class_name" = Kano;
subject = (
"subject_id" = 159;
"subject_name" = "Kano Class";
}
);
},
{
"alarm_cnt" = 0;
"class_id" = 71;
"class_name" = Lagos;
subject = (
"subject_id" = 160;
"subject_name" = "Lagos Class";
}
);
},
{
"alarm_cnt" = 3;
"class_id" = 73;
"class_name" = Nasarawa;
subject = (
"subject_id" = 208;
"subject_name" = "DOMA Class";
},
"subject_id" = 207;
"subject_name" = "EGGON Class";
},
"subject_id" = 206;
"subject_name" = "KARU Class";
},
"subject_id" = 209;
"subject_name" = "LAFIA Class";
},
"subject_id" = 161;
"subject_name" = "Nasarawa State Class";
}
);
},
{
"alarm_cnt" = 2;
"class_id" = 72;
"class_name" = Rivers;
subject = (
"subject_id" = 162;
"subject_name" = "Rivers Class";
}
);
}
)
I have tried this here is my snippet
- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RowCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *d=[_sitesJson objectAtIndex:0] ;
NSArray *arr=[d valueForKey:#"subject_name"];
NSDictionary *subitems = [arr objectAtIndex:0];
NSLog(#"Subitems: %#", subitems);
NSString *siteName = [NSString stringWithFormat:#"%#",subitems];
cell.textLabel.text =siteName;
//}
NSLog(#"Row Cell: %#", cell.textLabel.text);
// just change the cells background color to indicate group separation
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];
return cell;
}
- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section
{
static NSString *CellIdentifier = #"GroupCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:2];
NSDictionary *d2 = [_regionsJson objectAtIndex:0];
NSArray *arr2 = [d2 objectForKey:#"class_name"];
NSString *regions = [[arr2 objectAtIndex:section]objectAtIndex:0];
textLabel.textColor = [UIColor whiteColor];
textLabel.text = [NSString stringWithFormat: #"%# (%d)", regions, (int)[self tableView:tableView numberOfRowsInSection:section]];
NSLog(#"Group cell label: %#", textLabel.text);
// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:#"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;
}
He, just need to update one single method little bit way
- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section
{
static NSString *CellIdentifier = #"GroupCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSIndexPath *indexPath;
NSString *regions = [[_dataGroup objectAtIndex:section]objectAtIndex:0];
cell.textLabel.text = [NSString stringWithFormat: #"%# ", regions];
// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:#"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;
}
May help it you.
HTH, Enjoy Coding !!
I think you need to create a TableView which will have a sections array, and each sections row will be populated using the corresponding sections array. Tapping on a section will expand it and it's all rows will be visible.
To meet your requirements, you could follow the below steps as well -
1) Your modal should have a array for sections. The sections array will contain the sections objects, name of the section and corresponding array of the rows.
2) Implement the data source methods of the table view like
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [section count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50; // sections height
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0 , 0, tableView.frame.size.width , 50)] autorelease];
[view setBackgroundColor:[UIColor redColor]];
view.layer.masksToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5 , 2 , view.frame.size.width - 10 , view.frame.size.height - 3)];
label.text = ((SectionObject *)[section objectAtIndex:indexPath.section]).sectionName;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.textColor = [UIColor WwhiteColor];
label.clipsToBounds = YES;
label.font = [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:14.0f];
label.layer.masksToBounds = YES;
UIImageView *arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(view.frame.size.width - 30, 0, 17 , 17)];
[arrowImage setCenter:CGPointMake(arrowImage.center.x , (view.frame.size.height/2) ) ];
if(section == self.m_currentSelectedSection)
[arrowImage setImage:self.m_upArrowImage];
else
[arrowImage setImage:self.m_downArrowImage];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
button.tag = section;
[button addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
[view addSubview:label];
[label release];
[view addSubview:arrowImage];
[arrowImage release];
[view addSubview:button];
[button release];
view.clipsToBounds = YES;
return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger count = 0;
if(self.m_currentSelectedSection == section)
count = [((SectionObject *)[section objectAtIndex:indexPath.section]).rowArray count];
return count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40.0;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellId = #"cellIdentifier";
UITableViewCell *cell = nil;
cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//customize cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
When ever any section will be tapped following event will be invoked
- (void) sectionTapped:(UIButton *)button
{
self.m_currentSelectedSection = button.tag;
[self performSelector:#selector(refreshView) withObject:nil afterDelay:POINT_ONE_SECOND];
if(m_winnerSlotList->at(self.m_currentSelectedSection).m_leaderboardList.size())
[self.m_leaderboardTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];
UIView *baseView = [button superview];
if(baseView)
{
for(int ii = 0 ; ii < [[baseView subviews] count] ; ii++ )
{
UIView *anyView = [[baseView subviews] objectAtIndex:ii];
if([anyView isKindOfClass:[UIImageView class]])
[(UIImageView *)anyView setImage:self.m_upArrowImage];
}
}
}
Initialize self.m_currentSelectedSection = 0, for the first time, this will show the rows for 0th section. As any section is tapped it's rows will be visible (corresponding section rows will expand) and the rows for the previous selected section will be hidden(previous section rows will collapse).
If you need to show more than one section as expanded than you need to keep track of all the section whether a section is expanded or not and accordingly load show/ hide the cells for the corresponding section.

Programiticaly Multiple Columns in Tableview 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;
}

UITableViewController with two sections

I am trying to make a UITableviewController with two separate sections, one with some UIImages and the other with a UIButton. This is the code. The first section works perfectly, but if I set return 2 to the number of sections the app crashes with error:
'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
This is the code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0)
{
return [arrayOfMessages count];
}
else{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section==0)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
balloonView.tag = 1;
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.tag = 2;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:#"Chalkduster" size:14];
UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 0;
[message addSubview:balloonView];
[message addSubview:label];
message.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:message];
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];
}
else
{
balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:1];
label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2];
}
NSString *textAll = [arrayOfMessages objectAtIndex:indexPath.row];
NSString *textMessage = [textAll substringFromIndex:2];
NSString *textType = [textAll substringToIndex:2];
CGSize size = [textMessage sizeWithFont:[UIFont fontWithName:#"Chalkduster" size:14] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
if ([textType isEqualToString:#"ME"]) {
balloonView.frame = CGRectMake(318.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
balloonView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
balloon = [[UIImage imageNamed:#"GreenMessage.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
label.textColor = [UIColor whiteColor];
} else if ([textType isEqualToString:#"HE"]) {
balloonView.frame = CGRectMake(2.0, 2.0, size.width + 28, size.height + 15);
balloonView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
balloon = [[UIImage imageNamed:#"GrayMessage.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
label.frame = CGRectMake(16, 8, size.width + 5, size.height);
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
label.textColor = [UIColor blackColor];
}
balloonView.layer.cornerRadius = 5;
balloonView.clipsToBounds = YES;
balloonView.image = balloon;
label.text = textMessage;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *body = [arrayOfMessages objectAtIndex:indexPath.row];
CGSize size = [body sizeWithFont:[UIFont fontWithName:#"Chalkduster" size:14] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
How can I do it?
- (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];
// Your's code here
}
NSString *textAll = nil;
if (indexPath.section == 0)
{
// fetch array object here
textAll = [arrayOfMessages objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1)
{
// Since your array for section 1 returns only one array's object
// you should fetch it appropriately by specifying the index of the array
textAll = [arrayOfMessages objectAtIndex:"Specify the index of the array you want to fetch here which should be within the the bound of arrayOfMessages"];
}
// configure your cell here
return cell;
}
The error is not related to the sections. what you did is right. It's the NSArray arrayOfMessages that might be causing the crash.
In section 1 (second section) the number of rows is 1 according to your code:
if (section==0)
{
return [arrayOfMessages count];
}
else
{
return 1;
}
but what if there is no data in the NSArray arrayOfMessages?
NSString *textAll = [arrayOfMessages objectAtIndex:indexPath.row];
The code will break here.
The problem is you are not creating an instance of tableviewcell when tableview starts creating for section 2 remove if(indexPath.section == 0). In addition to this if you have section based tableview you need to store 2 dimentional array. First array is for section and items for each section. For insance
self.arrayOfMessages = #[ #[ #"Section_Text1" , #"Section1_Text2" ], #[ #"Section2_Text1" , #"Section2_Text2" , #"Section2_Text3" ] ];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayOfMessages[indexPath.section].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = self.arrayOfMessages[indexPath.section][indexPath.row];
///TODO
}

in tableview how dynamically changing section y position ios

The Screen Shot I added here i have dynamic data in my sections. Each section have one row and have label and values in it . my problem is when i add or change count of values its sections 'y' margin changes. please help me if anyone Know
here is my code
- (UIView *)tableView:(UITableView *)tableView1 viewForHeaderInSection:(NSInteger)section{
UIView *section1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView1.bounds.size.width, tableView1.bounds.size.height)] ;
[section1 setBackgroundColor:[UIColor clearColor]];
return section1;
}
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section !=0){
return 22;
}
return 0.0;
}
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 0.0;
}
//dynamic decide no of sections in table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(tagLastbutton == lastRow){
lastRow = lastRow +1;
return lastRow;
}
return lastRow;
//[self.arrayObjects count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
// Return YES to allow the user to reorder table view rows
//
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
// Return a cell containing the text to display at the provided row index.
//changing background according to input
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyuSocialNetworkCell";
CreateNewProfileViewCell * cell = (CreateNewProfileViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nibObjects= [[NSBundle mainBundle] loadNibNamed:#"CreateNewProfileViewCell" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass:[CreateNewProfileViewCell class]])
{
cell = (CreateNewProfileViewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//[indexPath.row color]
// if([self.subCategoryString length] != 0){
// cell.addFoodAllergyLabel.text = self.subCategoryString;
// }
NSString *str;
if(indexPath.section < [self.namesFoodSubCategoryLabels count]){
str = [self.namesFoodSubCategoryLabels objectAtIndex:indexPath.section];
}
int len=str.length;
NSLog(#"Ch ###########################%u",len);
len=floor(len/29);
NSLog(#"Ch ##After Row##################%u",len);
len++;
// len++;
/*
CGRect frameCell = CGRectMake(cell.frame.origin.x, cell.frame.origin.y+newCellPosition, cell.frame.size.width, cell.frame.size.height);
cell.frame = frameCell;
*/
CGRect frame = CGRectMake(cell.addFoodAllergyLabel.frame.origin.x, cell.addFoodAllergyLabel.frame.origin.y, cell.addFoodAllergyLabel.frame.size.width, cell.addFoodAllergyLabel.frame.size.height*len);
cell.addFoodAllergyLabel.numberOfLines = len;
cell.addFoodAllergyLabel.frame = frame;
CGRect frameMidBox = CGRectMake(cell.midMainRowBoxImageView.frame.origin.x, cell.midMainRowBoxImageView.frame.origin.y, cell.midMainRowBoxImageView.frame.size.width, cell.midMainRowBoxImageView.frame.size.height*len);
cell.midMainRowBoxImageView.frame = frameMidBox;
CGRect frameLowerCornerBox = CGRectMake(cell.lowerRowCornerImageView.frame.origin.x, cell.lowerRowCornerImageView.frame.origin.y+((len*26)-26), cell.lowerRowCornerImageView.frame.size.width, cell.lowerRowCornerImageView.frame.size.height);
newCellPosition = cell.lowerRowCornerImageView.frame.origin.y+22;
cell.lowerRowCornerImageView.frame = frameLowerCornerBox;
CGRect frameButtonImage = CGRectMake(cell.addFoodAllergyButton.frame.origin.x, cell.addFoodAllergyButton.frame.origin.y+((len * 13)-13), cell.addFoodAllergyButton.frame.size.width, cell.addFoodAllergyButton.frame.size.height);
cell.addFoodAllergyButton.frame = frameButtonImage;
cell.addFoodCategoryImageView.frame = frameButtonImage;
NSLog(#"Array&&&&&&&&&&&&&&&&%#",self.namesFoodSubCategoryLabels);
NSLog(#"indexPath.row %u",indexPath.row);
if(indexPath.section < [self.namesFoodSubCategoryLabels count]){
cell.addFoodAllergyLabel.text = [self.namesFoodSubCategoryLabels objectAtIndex:indexPath.section];
cell.addFoodCategoryImageView.image = [UIImage imageNamed:[self.imagesFoodCategories objectAtIndex:indexPath.section]];
}
//CGRect frame = CGRectMake(0, 0, 60, 60);
//cell.frame=frame;
cell.addFoodAllergyButton.tag = indexPath.section+1;
cell.addFoodAllergyLabel.tag = indexPath.section+1;
[cell.addFoodAllergyButton addTarget:self action:#selector(addFoodAllergyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(CGFloat) tableView:(UITableView *) tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
for(int i=0 ; i< [self.namesFoodSubCategoryLabels count] ; i++){
NSString *str = [self.namesFoodSubCategoryLabels objectAtIndex:i];
int len=str.length;
NSLog(#"Ch ######heightForRow#####################%u",len);
len=floor(len/29);
len++;
//len++;
NSLog(#"Ch ##After heightForRow###################%u",len);
return ((len*26)+11);
}
return 37;
}
I am changing dynamically background,label and section height but not able to handle sections y dynamically. so as screenshots shows that my problem is to manage sections margin in y position which is change between every two sections.
You can change the value of section on the basis of some condition like
if(section==0)
then "section1"
if(section==1)
then "section2"
because section starts from 0,1,2,3,4,5,6......n

how to change image when clicked on tableview cell iphone

i have a tableview. i add image to the cells when clicked on the cell in didselectrow method by using cell.conteview addsubview. but the problem is if i click on 1st cell it changes the image and when i click on another cell image will appears but the old image is not removed from the previous cell. This is happening for all cells in table view if a cell is clicked.
i used the code as follows
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Practices *bPractices = [topics objectAtIndex:indexPath.row];
UIImageView *clickView;
//[cell.contentView removeFromSuperview];
clickView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 315, 82)];
clickView.image = [UIImage imageNamed:#"list_bg_hover.png"];
[cell.contentView addSubview:clickView];
[clickView release];
UILabel *labelText= [[UILabel alloc]initWithFrame:CGRectMake(90, 30, 320, 20)];
labelText.text=bPractices.practices_title;
labelText.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:labelText];
}
pls help me how to solve this issue
Thanks in advance
I did something similar, but not with images but buttons. If a cell wasn't selected yet and gets tabbed, the size is changed and a certain and individual number of buttons is added. If another cell was selected, this one gets closed.
Code from form the UITableViewController
interface:
int openedCellIndex;//<-what cell is selected
int buttonCounter;
implementation:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( indexPath.row != openedCellIndex )
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]
autorelease];
//cell.frame = CGRectMake (0,0, 320, 100);
}
id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = article.name;
if ( self.cellBackgroundColor )
cell.backgroundColor = self.cellBackgroundColor;
return cell;
}
else {
//get article
id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];
//number of buttons
int buttons = [article.specification count];
int height = 50 * ceil(buttons / 2) + 50;
//construct special cell
UITableViewCell *cell = [[[TRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"CellBig"]
autorelease];
cell.frame = CGRectMake (0,0, 320, 150);
cell.textLabel.text = #"";
//add label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 30)];
label.font = [UIFont boldSystemFontOfSize:17];
label.text = article.name;
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
[label release];
if ( buttonMapper == nil )
self.buttonMapper = [NSMutableDictionary dictionaryWithCapacity:10];
//NSLog (#" bla: %#", article.isFreePrice);
//see if we have a free prized article
//create the buttons
NSEnumerator *enumerator = [article.specification objectEnumerator];
id <P_E_P1ArticleSpecification> spec;
int count = 0;
NSArray *specs =
[self sortedArticleSpecifications:article];
for ( spec in specs ) {
//see which row and col the button is in
int row = floor ( count / 2 );
int col = count % 2;
//define button position
int left = 20 + col * 145;
int top = 45 + row * 50;
//create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(left, top, 135, 40);
[cell addSubview:button];
[button setTitleColor:[UIColor blackColor] forState:0];
[button addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
//remember which article the button is attached to
buttonCounter++;
button.tag = buttonCounter;
[buttonMapper setValue:spec forKey:[NSString stringWithFormat:#"bla%d",buttonCounter]];
count++;
}
if ( self.cellBackgroundColor )
cell.backgroundColor = self.cellBackgroundColor;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( openedCellIndex == indexPath.row )
{
id <P_E_P1Article> article = [self.fetchedResultsController objectAtIndexPath:indexPath];
int count = [article.specification count];
int height = 50 * ceil(count / 2.) + 50;
return height;
}
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self openRow:indexPath.row];
[self.searchBar resignFirstResponder];
}
- (void) openRow:(int) index
{
if ( index == openedCellIndex ) return;
int oldIndex = openedCellIndex;
openedCellIndex = index;
NSUInteger indexArr[] = {0, oldIndex};
NSIndexPath *oldPath = [NSIndexPath indexPathWithIndexes:indexArr length:2];
NSUInteger indexArr2[] = {0, openedCellIndex};
NSIndexPath *newPath = [NSIndexPath indexPathWithIndexes:indexArr2 length:2];
//update view
[(UITableView *)self.view beginUpdates];
if ( oldIndex >= 0 )
[(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:oldPath]
withRowAnimation:UITableViewRowAnimationFade];
if (openedCellIndex >=0 )
[(UITableView *)self.view reloadRowsAtIndexPaths:[NSArray arrayWithObject:newPath]
withRowAnimation:UITableViewRowAnimationFade];
[(UITableView *)self.view endUpdates];
}
You could also subclass UITableViewCell and handle the select/deselect process in the setSelected method. Then use your custom cell as the type for the prototype cell instead of the default.

Resources