UICollectionView all sections are not displayed - ios

I am using UICollectionView and using RFQuiltLayout library. I am dynamically assigning the numberOfSectionsInCollectionView and numberOfItemsInSection. Right now 17 sections are getting assigned to the collection View. But while displaying it is displaying only one section.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"tableCell" forIndexPath:indexPath];
UILabel* label = (id)[cell viewWithTag:21];
if(!label) label=[[UILabel alloc] initWithFrame:CGRectMake(2, 0, cell.frame.size.width-4, cell.frame.size.height)];
label.backgroundColor=[UIColor grayColor];
label.tag=21;
label.textColor=[UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 2;
[label setFont:[UIFont fontWithName:#"HelveticaNeue" size:11]];
[cell.contentView addSubview:label];
NSLog(#"total section assigned %ld",(long)collectionView.numberOfSections);
NSLog(#"current section %ld",(long)indexPath.section);
if (indexPath.section == 0) {
if (indexPath.row == 0) {
label.text = #"Category";
[label setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:11]];
label.backgroundColor = [UIColor colorWithRed:127/255.f green:151/255.f blue:201/255.f alpha:1];
}else if (indexPath.row == 1) {
label.text = #"Current";
[label setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:11]];
label.backgroundColor = [UIColor colorWithRed:255/255.f green:176/255.f blue:142/255.f alpha:1];
}
}else {
CrimeAnalysisTable *aTable = [self.tableDataArray objectAtIndex:indexPath.section - 1];
if (indexPath.row == 0) {
label.text = aTable.category;
}
}
[cell setAutoresizesSubviews:YES];
cell.backgroundColor=[UIColor whiteColor];
}
[cell setNeedsLayout];
return cell;
}
I am using the above code to Display the section but the code is not going in the else part
the NSLog for number of section is printing 17 and for current section is printing 0. But the count is not going beyond 0 .

Related

How to show different labels in different sections?

I want to set labels in section... How can i set different labels in different section cz in tableview i want to make different sections and i am using the title for header in section.. by doing this i make different sections correctly but not able to differentiate the data between the sections.. Anyone can help me
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
// create the label object
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
headerLabel.backgroundColor = mainColor;
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textAlignment = UITextAlignmentCenter;
//
// UILabel *firstSection = [[UILabel alloc] initWithFrame:CGRectZero];
// firstSection.frame = CGRectMake(0,0,self.view.frame.size.width,60);
//
// firstSection.font = [UIFont boldSystemFontOfSize:18];
UIView *firstSection = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
//headerLabel.textAlignment = UITextAlignmentCenter;
if(section == 0)
headerLabel.text = #"Reciever Party";
UILabel *nameLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 35, 100 ,50)];
nameLbl.text =#"Name";
nameLbl.textColor=[UIColor blackColor];
UIFont *bold = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[nameLbl setFont:bold];
nameLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:nameLbl];
UILabel *addLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 65, 100 ,50)];
addLbl.text =#"Address";
addLbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold1];
addLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:addLbl];
if(section == 1)
headerLabel.text = #"Sender Party";
UILabel *senderName =[[UILabel alloc]initWithFrame:CGRectMake(20, 125, 100 ,50)];
senderName.text =#"Address";
senderName.textColor=[UIColor blackColor];
UIFont *bold2 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold2];
senderName.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:senderName];
if(section == 2)
headerLabel.text = #"Third Section Header";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
I am giving you a sample with hard coded data that how can you show data in section tableView. viewForHeaderInSection is responsible for creating headers of each section, not for showing data in section's row.
I am adding label on cell's content view as you don't want to add this from storyboard/ prototype cell.
Try this:-
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"YourIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"YourIdentifier"];
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100 ,50)];
lbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[lbl setFont:bold1];
lbl.hidden = NO;
lbl.tag = 101;
lbl.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:lbl];
}
UILabel *lblCell = (UILabel*)[cell.contentView viewWithTag:101];
if(indexPath.section == 0) //first section
{
if (indexPath.row == 0) //first row of first section
{
lblCell.text =#"Name";
}
if (indexPath.row == 1) //second row of first section
{
lblCell.text =#"Address";
}
}
if(indexPath.section == 1) //second section
{
if (indexPath.row == 0) //first row of second section
{
lblCell.text =#"age";
}
if (indexPath.row == 1) //second row of second section
{
lblCell.text =#"phone no.";
}
}
if(indexPath.section == 2) //third section
{
if (indexPath.row == 0) //first row of third section
{
lblCell.text =#"city";
}
if (indexPath.row == 1) //second row of third section
{
lblCell.text =#"father";
}
}
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
you have to use like that
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 225, 40)];
UIImageView *sectionImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
sectionImage.image = [UIImage imageNamed:[objMenuImageList objectAtIndex:section]];
sectionImage.contentMode = UIViewContentModeCenter;
[sectionView addSubview:sectionImage];
UILabel *sectionLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 150, 20)];
sectionLable.contentMode = UIViewContentModeLeft;
sectionLable.text = [objMenuList objectAtIndex:section];
[sectionLable setFont:[UIFont systemFontOfSize:14]];
sectionLable.textColor = [UIColor colorWithRed:(206/255.f) green:(180/255.f) blue:(116/255.f) alpha:1.0f];
[sectionView addSubview:sectionLable];
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
sectionButton.frame = CGRectMake(0, 0, sectionView.frame.size.width, sectionView.frame.size.height -10);
sectionButton.center = sectionView.center;
sectionButton.tag = section;
// sectionButton.backgroundColor = [UIColor yellowColor];
[sectionButton addTarget:self action:#selector(ExpandCell:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];
sectionView.backgroundColor = [UIColor clearColor];
return sectionView;
}
Just keep It simple And Flexible.
Do it like this..
Step 1. Add one custom cell For Header View with UILabel for title.
Step 2. In cellForRowAtIndexPath , dequeueReusableCellWithIdentifier this cell .
Use condition as...
` if(section==0){
Cell.title.text= #"your title for section 0"
} // here title is UILabel `
By doing this your code is Flexible enough for Any Future Changes!!!

How to Add footer text only in text last section of the tableview?

I want to display footer text only at last section of the TableView.In TableView I have a lot of TableView section it come from API.But I need to display footer message only at bottom of the tableView section how to do it?
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10.0;
}
Currently Im used this code.This shows in all section bottom.I need to display only at bottom of the last tableview section.
try this
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 55)];
if (section == array.count -1){
footer.backgroundColor=[UIColor clearColor];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,540,40)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #" Please add your message";
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setNumberOfLines:10];//set line if you need
[lbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14.0]];//font size and style
[lbl setTextColor:[UIColor blackColor]];
[footer addSubview:lbl];
self.jobsTableView.tableFooterView=footer;
}
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
return 60.0;
} else {
return 0.0;
}
}
use
if (section == yourArray.count -1)
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
}
return footer;
Try this,
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == [tableView numberOfSections] - 1) {
return 10.0;
} else {
return 0.0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
UIView *footer = ..
....
return footer;
} else {
return nil;
}
}
or you can set view as footer to tableview
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
....
[self.tableView setTableFooterView:footer];
Please try to use like this if you wanna footerview of tableview.
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
footer.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footer;

UITableView cells don't show labels and buttons

I have a UITableView that has its delegate and dataSource properties set correctly, however its content isn't showing.
MY Code
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BOOL notInputNotes;
UIColor *normalBlue = [[UIColor alloc]initWithRed:0.1 green:0.65 blue:1.0 alpha:0.8];
HomeWork *hw = [hwArray objectAtIndex:indexPath.row];
NSString *dateDueAsText = [dateFormat stringFromDate:hw.dateDue];
CGFloat xForDate;
if (dateDueAsText.length < 16 || dateDueAsText.length == 16) {
xForDate = 215;
}
else{
xForDate = 214;
}
hw.indexPath = indexPath;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.subviews) {
[view removeFromSuperview];
}
if (!isEditing) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setEditing:NO];
}
else{
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
[cell setEditing:YES];
}
UILabel *subject = [[UILabel alloc]initWithFrame:CGRectMake(70, 5, 150, 45)];
if(![hw.subject isEqualToString:#""])
{
subject.text = hw.subject;
notInputNotes = NO;
}
else{
subject.text = hw.notes;
subject.frame = CGRectMake(70, 20, 150, 45);
notInputNotes = YES;
}
[subject setFont:[UIFont systemFontOfSize:20]];
[subject setTextColor:normalBlue];
subject.adjustsFontSizeToFitWidth = YES;
subject.backgroundColor = [UIColor blueColor];//test
[cell addSubview:subject];
if(!notInputNotes){
UILabel *notes = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 300, 30)];
notes.text = hw.notes;
[notes setFont:[UIFont systemFontOfSize:12]];
notes.adjustsFontSizeToFitWidth = YES;
[notes setTextColor:[UIColor colorWithRed:0.2 green:0.3 blue:1.0 alpha:1.0]];
[cell addSubview:notes];
}
UILabel *importancy = [[UILabel alloc]initWithFrame:CGRectMake(300, 45, 200, 30)];
importancy.text = hw.importancy;
[importancy setFont:[UIFont systemFontOfSize:12]];
[importancy setTextColor:[UIColor redColor]];
importancy.adjustsFontSizeToFitWidth = YES;
[cell addSubview:importancy];
UILabel *dateDue = [[UILabel alloc]initWithFrame:CGRectMake(xForDate, 10, 200, 30)];
dateDue.text = [NSString stringWithFormat:#"%#",dateDueAsText];
[dateDue setFont:[UIFont systemFontOfSize:13]];
dateDue.textColor = normalBlue;
dateDue.adjustsFontSizeToFitWidth = YES;
[cell addSubview:dateDue];
hw.doneButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
if(!hw.done){
[hw.doneButton setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else{
[hw.doneButton setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
[hw.doneButton addTarget:hw action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nowMinus1 = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSComparisonResult result;
result = [nowMinus1 compare:hw.dateDue];
if(result==NSOrderedDescending && !(hw.done)){
cell.backgroundColor = [UIColor colorWithWhite:0.86 alpha:0.6];
NSLog(#"%# is earlier than %#",dateDueAsText,[dateFormat stringFromDate:nowMinus1]);
[hw.doneButton setImage:[UIImage imageNamed:#"lighter-gray_unckecked.jpg"] forState:UIControlStateNormal];
hw.late = YES;
}
else{
cell.backgroundColor = [UIColor whiteColor];
hw.late = NO;
}
[cell addSubview:hw.doneButton];
return cell;
}
Does somebody know what's wrong there? Thanks in advance.
Note
the cell's background color appears different if you set it, so the cells are there and re correctly made the problem is really with the views
I wouldn't say it is a really good practice to remove all subviews of a UITableViewCell and add your own subviews after. It would be better to subclass UITableViewCell.
That being said, you can try this instead, it will display your content.
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
But your problem definitely comes from you wanting to remove the subviews. If the first problem is cell overlapping try implementing
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return customHeight;
}

How to make tableView height change for two rows in iphone

I have tableView in which I want show the first cell only which is textfield as a subview of cell. It works fine when I enter any data in it. It shows entered data in new line mean second row and first row is same field but, when I enter another rows then tableVIew height gets messy.
I want that when I add second row it should show scroll instead of increasing height.
Here is the tableView code:
-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//cell.backgroundColor = [UIColor clearColor];
}
for (UIView *subView in cell.subviews)
{
if (subView.tag == 2 || subView.tag == 22)
{
[subView removeFromSuperview];
}
}
tableView.backgroundView=nil;
if(indexPath.section==0)
{
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,0,248,31)];
tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
tagInputField.textAlignment=UITextAlignmentLeft;
//tagInputField.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"inputfeild2.png"]];
tagInputField.backgroundColor=[UIColor whiteColor];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
// Jani Comment New tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// jani [tagInputField setBorderStyle:UITextBorderStyleRoundedRect];
[tagInputField.layer setCornerRadius:5.0f];
[tagInputField.layer setMasksToBounds:YES];
tagInputField.layer.borderWidth = 0.5;
tagInputField.layer.borderColor = [UIColor darkGrayColor].CGColor;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#"Enter tag here "];
tagInputField.textColor =[UIColor grayColor];
[cell addSubview:tagInputField];
return cell;
}
if(indexPath.section==1)
{
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
crossButton.tag = 22; //use a tag value that is not used for any other subview
//crossButton.backgroundColor = [UIColor purpleColor];
crossButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Cross.png"]];
[cell addSubview:crossButton];
cell.textLabel.font =[UIFont fontWithName:#"Myriad-Pro" size:8];
cell.textLabel.textColor =[UIColor grayColor];
cell.backgroundColor=[UIColor whiteColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
[tagInputField setFrame:CGRectMake(0,0,248,30)];
// jani [tagInputField setFrame:CGRectMake(8,2,248,30)];
tableView.backgroundColor=[UIColor whiteColor];
// tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[publishButton setFrame:CGRectMake(40,560,250, 50)];
[descriptionTextImageView setFrame:CGRectMake(48,450,250,90)];
[tagInputField.layer setCornerRadius:0.0f];
[tagInputField.layer setMasksToBounds:YES];
tagInputField.layer.borderWidth = 0.0;
tagInputField.layer.borderColor = [UIColor clearColor].CGColor;
return cell;
}
Here is the code when height changes
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField.tag == 2)
{
if (textField.text.length > 0 || ![tagTextField.text isEqualToString:#""]) {
[textField resignFirstResponder];
[tagArray addObject:tagInputField.text];
[tableView reloadData];
CGRect frame = tableView.frame;
frame.size.height = MIN(64 * [tagArray count], 400); // 400 is the maximum heighthat the table view can have. You can change it to whatever you like
tableView.frame = frame;
[tableView.layer setCornerRadius:7.0f];
[tableView.layer setMasksToBounds:YES];
tableView.layer.borderWidth = 0.5;
tableView.layer.borderColor = [UIColor grayColor].CGColor;
[self showAnimationBack];
}
else {
// textField.text = #"Enter tag";
[textField resignFirstResponder];
[self showAnimationBack];
}
}
if (textField == titleTextField) {
[titleTextField resignFirstResponder];
}
if (textField == descriptionTextView) {
[descriptionTextView resignFirstResponder];
[self showAnimationBack];
}
return YES;
}
Set the indexPath.row with specific number of row to change height of specific cell, such like..
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.row == 0) /// here you need to set row number
{
return 90;
}
else if (indexPath.row == 2)
{
return 110;
}
return 45;
}

UITableview , dynamically section & rows

want create a uitableview dynamically with 2 rows in one section
i write this code but i have problem the repeats only 2 first rows in all sections
now i have this row 0,1 -> section 0 , row 0,1 -> section 1 , row 0,1 -> section 2
want read all rows after for example row 0,1 -> section 0 , row 2,3 -> section 1 , row 4,5 -> section 2 -> row 6,7
items = [[NSMutableArray alloc] init];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return items.count/2; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return 2; }
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
label.textColor = [UIColor darkGrayColor];
[label setAlpha:0.8];
cell.backgroundColor = [UIColor whiteColor];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN-5, CELL_CONTENT_MARGIN+30, CELL_CONTENT_WIDTH - ((CELL_CONTENT_MARGIN * 2)+6), MAX(size.height, 44.0f))];
label.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
for (UIView *theSubview in [cell.contentView subviews])
{
if([theSubview isKindOfClass:[UIImageView class]])
{
[theSubview removeFromSuperview];
}
}
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(128,5, 32, 32)];
imv.image=[UIImage imageNamed:#"0England.png"];
[cell.contentView addSubview:imv];
[imv release];
if (indexPath.row % 2==0) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(-7 ,10, 8, 18)];
img.image=[UIImage imageNamed:#"...."];
[cell.contentView addSubview:img];
[img release];
}
else { }
return cell;
}
You have a problem in your mapping of indexPaths to the rows of your data structure (in this case, your items array). Each section will be numbered from 0 to ceil(n/2) (which, by the way, should be the number of sections, not simply n/2) where n is the number of elements in items.
Each row inside the section will have an indexPath.row of 0 or 1.
Therefore, in order to map from indexPaths back to your items array, you have to do something like this in your cellForRowAtIndexPath function:
NSString *text = [items objectAtIndex:([indexPath section]*2 + [indexPath row])];

Resources