I am creating an application and I have a customized header of tableview. But sometimes the header gets duplicated. It takes the place of a row in tableview. How do I solve this strange problem? BTW, my tableviewstyle is plain, Header height is 44.0 and the footer height is 0.0. Here is an image how it displays. The header "comments" is duplicated just below the header "Messages" while it should be the row.
Here is the complete implementation of this view
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
TitleForSectionView=[[NSArray arrayWithObjects:#"Dates To Remember",#"Messages",#"Comments",#"Wishlist",#"Reminders",#"Bookmarks",nil] retain];
self.MySectionIndexArray=[[NSMutableArray alloc] init];
[self.MySectionIndexArray addObject:#"UP"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
[self.MySectionIndexArray addObject:#"DOWN"];
self.IconsForSectionsView=[[NSMutableArray alloc] init];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconCalender.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconMessage.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconComments.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconWishList.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconReminder.png"]];
[self.IconsForSectionsView addObject:[UIImage imageNamed:#"IconBookMark.png"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:#"UP"]) {
return 4;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *MyDashBoardCell=nil;
static NSString *AddNewDateCellIdentifier=#"AddNewDateCell";
static NSString *CellIdentifier = #"DashBoardCell";
DashBoardCustomCellObject = (DashBoardCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (DashBoardCustomCellObject == nil) {
[[[[NSBundle mainBundle] loadNibNamed:#"DashBoardCustomCell" owner:self options:nil] retain]autorelease];
}
[DashBoardCustomCellObject SetDashBoardCellData:#"Mar 9" EventText:#"My Birthday"];
AddNewDateCellObject = (AddNewDateCell *)[tableView dequeueReusableCellWithIdentifier:AddNewDateCellIdentifier];
if (AddNewDateCellObject == nil) {
[[[[NSBundle mainBundle] loadNibNamed:#"AddNewDateCell" owner:self options:nil] retain]autorelease];
}
if(indexPath.section==0 && indexPath.row==0)
{
MyDashBoardCell=AddNewDateCellObject;
}
else
{
MyDashBoardCell=DashBoardCustomCellObject;
}
return MyDashBoardCell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label and button and icon image
self.customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,44)] autorelease];
self.customView.backgroundColor=[UIColor whiteColor];
// create image object
UIImageView *icon= [[[UIImageView alloc] initWithImage:[self.IconsForSectionsView objectAtIndex:section]] autorelease];
icon.contentMode=UIViewContentModeCenter;
icon.frame = CGRectMake(0,0,40,40);
// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(53,11,172,21);
headerLabel.text = [TitleForSectionView objectAtIndex:section];
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor=[UIColor whiteColor];
// create the button objects
ButtonDrop = [[[UIButton alloc] initWithFrame:CGRectMake(278, 9, 25, 25)] autorelease];
ButtonDrop.tag=section;
if ([[self.MySectionIndexArray objectAtIndex:section] isEqualToString:#"UP"])
{
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Up.png"] forState:UIControlStateNormal];
self.customView.backgroundColor=[UIColor blueColor];
headerLabel.highlighted=YES;
}
else
{
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Down.png"] forState:UIControlStateNormal];
}
[ButtonDrop addTarget:self action:#selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
[self.customView addSubview:icon];
[self.customView addSubview:headerLabel];
[self.customView addSubview:ButtonDrop];
return self.customView;
}
- (void)checkAction:(id)sender
{
UIButton *Button = (UIButton*)sender;
NSLog(#"Button Tag=%d",Button.tag);
if([[self.MySectionIndexArray objectAtIndex:Button.tag] isEqualToString:#"UP"])
{
[self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:#"DOWN"];
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Up.png"] forState:UIControlStateNormal];
}
else
{
[self.MySectionIndexArray replaceObjectAtIndex:Button.tag withObject:#"UP"];
[ButtonDrop setBackgroundImage:[UIImage imageNamed:#"Down.png"] forState:UIControlStateNormal];
}
[TableDashBoard reloadSections:[NSIndexSet indexSetWithIndex:Button.tag] withRowAnimation:UITableViewRowAnimationFade];
}
Try having a zero-height row for your empty sections. That seems to make them draw correctly for me.
I think there are a couple of explainations:
(1) You're displaying the section header of an empty section which would cause two headers to appear one on top of the other with no rows in between.
(2) You are returning a header view as a row cell in – tableView:cellForRowAtIndexPath: Since the header "it takes place of a row in tableview" I think this the most likely explanation. Check – tableView:viewForHeaderInSection: as well.
Related
My recent frustration is a UIButton subview in each UITableViewCell of my UITableView which I want to setHidden: according to a specific clause for each indexPath. My code is pretty much the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[self initCell:cell forIndexPath:indexPath];
}
[self updateCell:cell forIndexPath:indexPath];
return cell;
}
and the init and update methods are the following:
- (void)initCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
...
UIButton *btnMy = [UIButton buttonWithType:UIButtonTypeCustom];
btnMy.tag = kButtonMyTag;
[btnMy setFrame:CGRectMake(170, 45, 100, 30)];
[btnMy setBackgroundImage:[UIImage imageNamed:#"btn_image"] forState:UIControlStateNormal];
btnMy.adjustsImageWhenHighlighted = YES;
[btnMy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnMy.titleLabel.font = [UIFont fontWithName:#"MyFont" size:14];
[btnMy addTarget:self action:#selector(btnMyPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnMy];
UIImageView *imgViewAccessory = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table_accessory"]];
cell.accessoryView = imgViewAccessory;
[imgViewAccessory release];
}
- (void)updateCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UIButton *btnMy = (UIButton *)[cell viewWithTag:kButtonMyTag];
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition)
{
btnMy.hidden = NO;
}
else
{
btnMy.hidden = YES;
}
...
}
The frustrating result is that when scrolling the button shows and hides randomly and not as expected according the if clause in the updateCell method.
Any help would be much appreciated. Thanks in advance!
You should make custom cell and depending upon the condition show and hide the button
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *nib;
static NSString *cellIdentifier= #"cell";
UITableViewCell *theCell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if([theCell.contentView subviews]){
for(UIView *view in [theCell.contentView subviews]){
[view removeFromSuperview];
}
}
if(theCell== nil)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"Your custom cell name" owner:self options:nil];
theCell = [nib objectAtIndex:0];
theCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIButton *btn=(UIButton*)[theCell.contentView viewWithTag:101];
if(yourcondition)
//hide button
else
//show button
}
This will do
Use this code in your CellForRowAtIndexPath Also.
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition) {
btnMy.hidden = NO;
}
else {
btnMy.hidden = YES;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITableView create 4 square
I want to have 4 cell in one row in uiTableview but nothing appear, I don't know why would you please help me
this a the picture for cell
Thanks in advance
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *monKey = #"Monday";
NSString *tueKey = #"Tuesday";
NSString *wedKey = #"Wednday";
NSString *thuKey = #"Thusday";
NSString *friKey = #"Friday";
NSString *satKey = #"Satuarday";
NSString *sunKey = #"Sunnday";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", #"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", #"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:sunKey];
[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
return cell;
}
Edit 1
this is the picture when I added this line
aBackgroundImageView.backgroundColor = [UIColor redColor];
Edit 2 :
with Rog method
It seems to me that in:
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
you are not providing a content for your views...
so that should explain why you don't see anything... Try adding:
aBackgroundImageView.backgroundColor = [UIColor redColor];
and see if anything changes.
Here i create the one demo application like you want and that some of the code put into here.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger devide=[self.tableArray count] / 3;
if ([self.tableArray count] % 3 >0 ) {
devide+=1;
} else {
devide=devide;
}
return devide;
}
And cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[[NSBundle mainBundle] loadNibNamed:#"CCell" owner:self options:nil] objectAtIndex:0];
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
[imgV1 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV2 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV3 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
HJManagedImageV *img1=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV1.frame.size.width, imgV1.frame.size.height)];
HJManagedImageV *img2=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV2.frame.size.width, imgV2.frame.size.height)];
HJManagedImageV *img3=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV3.frame.size.width, imgV3.frame.size.height)];
img1.tag=img2.tag=img3.tag=10000;
img1.backgroundColor=img2.backgroundColor=img3.backgroundColor=[UIColor clearColor];
[imgV1 addSubview:img1];
[imgV2 addSubview:img2];
[imgV3 addSubview:img3];
}
int x=indexPath.row;
x=x*3;
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
HJManagedImageV *img1=(HJManagedImageV*)[imgV1 viewWithTag:10000];
HJManagedImageV *img2=(HJManagedImageV*)[imgV2 viewWithTag:10000];
HJManagedImageV *img3=(HJManagedImageV*)[imgV3 viewWithTag:10000];
[img1 clear];
[img2 clear];
[img3 clear];
[imgV1 setTitle:[self.tableArray objectAtIndex:x] forState:UIControlStateDisabled];
img1.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x]];
[img1 showLoadingWheel];
[GlobalCacheManager manage:img1];
if((x+1) >= self.tableArray.count){
imgV2.hidden=imgV3.hidden=YES;
[imgV2 setBackgroundImage:nil forState:UIControlStateNormal];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else if( (x+2)>=self.tableArray.count) {
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[img2 showLoadingWheel];
[GlobalCacheManager manage:img2];
imgV1.hidden=imgV2.hidden=YES;
imgV3.hidden=YES;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else {
[img2 showLoadingWheel];
[img3 showLoadingWheel];
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[GlobalCacheManager manage:img2];
img3.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+2]];
[GlobalCacheManager manage:img3];
imgV2.hidden=imgV3.hidden=NO;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setTitle:[self.tableArray objectAtIndex:x+2] forState:UIControlStateDisabled];
}
return cell;
}
Your method is a bit of a mess. Your main problem is how you are setting the frame origin for the UIImageView instances as they are 1) too big for the cell 2) out of the cell bounds.
Try this (ps: there may be typos as I've written it off the top of my head):
- (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]
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int numberOfColumns = 4;
CGSize gridSize = CGSizeMake(cell.contentView.frame.size.width / numberOfColumns, cell.contentView.frame.size.height / 2); // Height size is arbitrary here, width is obtained by dividing total width by number of columns
for (int i=0; i < column; i++)
{
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(gridSize.width * i, cell.contentView.frame.size.height/2 - gridSize.height/2, gridSize.width, gridSize.height)];
aview.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1/i+1]; // This is just so you can see the different gridCells within the cell
[cell.contentView addSubview:aView];
[aView release];
}
return cell;
}
I have created a UITableView with two cells, username and password using the code below.
I have included a screenshot of the output the first time the view loads (the expected output) and the second time the view is shown (the incorrect output)
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Login Ident"];
if (indexPath.row == 0) {
loginId = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
loginId .placeholder = #"Email address";
loginId .autocorrectionType = UITextAutocorrectionTypeNo;
[loginId setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = loginId;
}
if (indexPath.row == 1) {
password = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
password.placeholder = #"Password";
password.secureTextEntry = YES;
password.autocorrectionType = UITextAutocorrectionTypeNo;
[password setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = password;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
loginId.delegate = self;
password.delegate = self;
[loginId setText:[keychain objectForKey:(__bridge id)kSecAttrAccount]];
[password setText:[keychain objectForKey:(__bridge id)kSecValueData]];
[loginId addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[password addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:loginId];
[cell.contentView addSubview:password];
}
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_tableView reloadData];
}
My UITextField declarations
UITextField *loginId;
UITextField *password;
If anybody is able to help me out with this, I'd really appreciate it.
With changes
You should put the code where you create the text fields inside the if (cell == nil) {} since otherwise you could be adding a second textfield to a cell that's already been created previously. If that doesn't fix the issue post back.
Also, remove:
[_tableView addSubview:loginId];
[_tableView addSubview:password];
You shouldn't add views that are meant for the cells to the tableview. And instead of making it the accessoryView of the cell, add them to the contentView
[cell.contentView addSubview:loginId];
See chat below for full resolution of the issue.
I have a table view and some buttons inside it. 4 buttons in one row then another 4 in second row and so one. I am using array for adding images to the buttons.
For the first time it is working properly but when delete all the items in the array and new objects to the array and then call reload table view, the new data is not reloaded.
What could be the reason?
I am posting a part of my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button1 setImage:[array objectAtindex:0]
[button1 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:self.button1];
button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button2 setImage:[array objectAtindex:1] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
}
else if(indexPath.row == 1)
{
button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button3 setImage:[array objectAtindex:2] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button4 setImage:[array objectAtindex:3] forState:UIControlStateNormal];
[button4 addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button4];
}
else
{
// Do something here
}
return cell;
}
my click button:
-(IBAction)clickToDisplayImage {
[text resignFirstResponder];
if([imageArray count] >0)
{
[imageArray removeAllObjects];
}
int totalImages = [text.text intValue];
for( int i=0;i<totalImages;i++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
}
[tableView reloadData];
}
When you call reloadData on your UITableView, your cellForRowAtIndexPath message will be called to populate the row. Because you have already created the cells from the first pass, when you call dequeueReusableCellWithIdentifier you will get back your originally created cells associated with the #"Cell" identifier.
You can confirm this by setting a breakpoint and checking that the second time through you will get your cell from dequeueReusableCellWithIdentifier (rather than a newly created cell). You will then be adding new subviews to that original cell (so four button subviews for the cell, then six, eight etc...).
What you should be doing is only adding the button subviews when you first create the cell (so move the button create stuff into the if (cell == nil) block) and you should be using a cell identifier based on the table row. Something like:
- (UIButton*) getButtonLeft:(BOOL)left atIndex:(int)index
{
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake((left ? 15 : 135),5,100,87)];
[button setImage:[array objectAtindex:index] forState:UIControlStateNormal];
[button addTarget:self action:#selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// both rows are different so we should use a different re-use id.
NSString * CellIdentifier= [NSString stringWithFormat:#"Cell%d", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
switch (indexPath.row)
{
case 0:
button1 = [self getButtonLeft:YES atIndex:indexPath.row];
[cell.contentView addSubview:button1];
break;
case 1:
button2 = [self getButtonLeft:NO atIndex:indexPath.row];
[cell.contentView addSubview:button2];
break;
case 2:
button3 = [self getButtonLeft:YES atIndex:indexPath.row];
[cell.contentView addSubview:button3];
break;
case 3:
button4 = [self getButtonLeft:NO atIndex:indexPath.row];
[cell.contentView addSubview:button4];
break;
default:
// other cells?
break;
}
}
// if you need to modify the cell you have dequeued/created this is where you would do that
return cell;
}
N.B. I haven't run this code through a compiler, so ware syntax errors ;)
I have a table view, and I am adding four UIImageViews to a row, so in total I have five rows and twenty image views. I am inserting images one by one into the cell. The second row will only start to fill once all four image views are filled.
I need some logic for this. I mean, how can I check which place is next to be filled in the table view and at which event? How can I add images one by one to the UIImageViews? Also, initially only two rows will be shown. After filling these two rows, when images begin to enter into the third row, I need to show an icon adjacent to the last row that is on the screen, to tell the user that there is more data below. Also, is there any event to detect which row is currently the last row on screen?
Also is there any way to individually select the images from a row?
Run this code:-
.h file code
#import <UIKit/UIKit.h>
#interface helpTableViewController : UIViewController {
IBOutlet UITableView *gridTableView;
NSMutableArray *imagesArray;
int widthview,widthview1;
}
#property(nonatomic,retain)NSMutableArray *imagesArray;
#property(nonatomic,retain)UITableView *gridTableView;
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath;
-(void)crossViewButtonClicked:(UIButton *)sender;
#end
.m file code:-
#synthesize gridTableView;
#synthesize imagesArray;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
self.imagesArray=tempArray;
[tempArray release];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic4.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic5.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic6.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
widthview=0;
widthview1=0;
}
- (void)viewDidUnload {
self.gridTableView=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[imagesArray release];
[gridTableView release];
[super dealloc];
}
#pragma mark -
#pragma mark TableView Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil) {
cell =[self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
profileimageView.tag=i+90;
[profileView addSubview:profileimageView];
[profileimageView release];
[scrollView addSubview:profileView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(widthview1+10, 12, 95, 155)];
//button.contentMode = UIViewContentModeScaleAspectFill;
button.tag=999999;
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
widthview=widthview+105;
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview1+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
// [profileimageView setImage:[UIImage imageNamed:#"gridimage1.png"]];
profileimageView.tag=4+i;
[profileimageView setImage:[self.imagesArray objectAtIndex:3+i]];
[profileView addSubview:profileimageView];
[scrollView addSubview:profileView];
[profileimageView release];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark -
#pragma mark button methods
-(void)imageButtonClicked:(UIButton *)sender
{
[self.imagesArray replaceObjectAtIndex:0 withObject:[UIImage imageNamed:#"abdulprofilepic.png"]];
[self.gridTableView reloadData];
}
You should keep all of the logic (and data) in your table view controller (UITableViewController subclass). I suggest you have a custom UITableViewCell subclass that can hold 4 images (the cell itself should be responsible for the layout), and the table view controller is responsible for setting it up. In -tableView:cellForRowAtIndexPath: you can look at your collection of images, then just use modular arithmetic to figure out which images should be in the cell for that particular index path.
As for individually selecting images, perhaps you could have your cells know their index path (set as a custom property in -tableView:cellForRowAtIndexPath:), then each have a delegate method telling your controller that a certain image was pressed (for example, -cellAtIndexPath:didSelectImageAtIndex:).
try this,
As there are fix number of images that you want to show in your row i. e. 4
So what you have to do is:-
in .h file
int widthview,widthview1;
in .m file in viewdidload
widthview=0;
widthview1=0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:4+i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}
return cell;
}
This code is for 2 rows do the same thing for next three rows.
Adjust the coordinates according to your requirement.
hope this will save your time.
answer for Also is there any way to individually select the images from a row? :
Give image to button and add that button inside row!!