Why, when I create a CollectionView, and dequeque two cells with different identifier, the latter are attached to each other?
The cells with the same identifier have equal distance between them, instead the cells with different identifier are stuck together.
Here my code:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
if (section == 0) {
return 1;
}
if (section == 1) {
return 10;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
UICollectionViewCell *cell;
if (indexPath.section == 0) {
cell = [cv dequeueReusableCellWithReuseIdentifier:#"AddCell" forIndexPath:indexPath];
UIImageView *cellImage = (UIImageView *)[cell viewWithTag:1];
[cellImage setImage:[UIImage imageNamed:#"pulsante_trasparente.png"]];
}
else if (indexPath.section == 1){
cell = [cv dequeueReusableCellWithReuseIdentifier:#"PictureCell" forIndexPath:indexPath];
UIImageView *cellImage = (UIImageView*)[cell viewWithTag:1];
[cellImage setImage:[UIImage imageNamed:#"mi.png"]];
}
return cell;
}
Please help me!
Implement this method to get space between cells of different sections:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
{
return UIEdgeInsetsMake(Top_Gap, Left_Gap, Buttom_Gap, Right_Gap);
}
Have you tried to set the minimum Spacing for cells at a negative value if not do so by going to the UICollectionView in your xib and selecting the inspector and customizing the minimum space there. I have a image for you showing what I mean:
Related
I have UICollectionview with sections and two cell class.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCell *cell;
firstDayInMonth = [self dayWeekStart:[self getDateFromItem:dateFromStart section:indexPath.section row:1]];
if (indexPath.row < firstDayInMonth) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellCalendarEmpty" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellCalendar" forIndexPath:indexPath];
}
}
I have start item, section and period. Beginning from the start section and item i need to change background color cell depending on period. i need to change only cellCalendar. I use cellCalendarEmpty to move first cellCalendar.
Fix by handling both branches of the case (all cases) when you configure, e.g.:
if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){
cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
cell.titleLabel.textColor = [UIColor whiteColor];
} else {
cell.backgroundColor = [UIColor whiteColor]; // whatever the default color is
}
If you're using a custom subclass of UICollectionViewCell, you may also reset to defaults by implementing the prepareForReuse method.
I am using these following codes to display multiple images on a table view. One row need to display 4 only images
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
//Resize auto complete table based on how many elements will be displayed in the table
return (int)ceil([wordsInSentence count]/4.0);
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"entered forRowAtIndexPath");
if ((indexPath.row % 2) == 0) {
cell.backgroundColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor whiteColor];
}
}
On the button click wich will display table view I wrote the below code
-(IBAction)setImageOnTableView
{
NSLog(#"entered setImageOnTableView");
j=1;
tableView.hidden=NO;
//Breaking the sentence into words and then placing it in an array
wordsInSentence=[[youSaid.text uppercaseString] componentsSeparatedByString:#" "];
[wordsInSentence retain];
[youSaid resignFirstResponder];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSLog(#"entered cellForRowAtIndexPath");
//j=1;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
[cell.contentView addSubview:nil];
for (int i=0; i <= [wordsInSentence count]; ++i) {
UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 20, 70, 100)] autorelease] ;
imageView1.tag = i+1;
[imageViewArray insertObject:imageView1 atIndex:i];
[cell.contentView addSubview:imageView1];
//[imageView1 release];
}
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [wordsInSentence count] % 4;
}
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self **showImage**:imageView];
}
return cell;
}
//method used to show the images on the table view by checking the category
//and images in the category
-(void)**showImage**:(UIImageView *)imageView
{
NSLog(#"entered showImage");
//this method will check if more than 1 image is present in the category
if([self searchImagesInSameCategory:[wordsInSentence objectAtIndex:j-1]]>1)
{
UIButton *descriptiveButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[descriptiveButton setFrame:CGRectMake(50, 0, 30, 30)];
[imageView setUserInteractionEnabled:TRUE];
[descriptiveButton addTarget:self action:#selector(showPopOver1:) forControlEvents:UIControlEventTouchUpInside];
descriptiveButton.tag=j;
[imageView addSubview:descriptiveButton];
globalString=[wordsInSentence objectAtIndex:j-1];
}
}
In this code when I displayed 4 images it will display 4. If I displayed 3 images after displaying 4 images, 3 images are displaying but still 4 th image is presenting in table view. How can I fix this. Please can any one help me...
The main problem is, an image view that displayed before is not removing from the cell.
Table cells can be (and in your code, are) cached and reused - which means that for every call to cellForRowAtIndexPath:: you should make sure that the cell you are returning is correct (has the correct images). In your case, I would simply clear the cell of all subviews/image views and recreate them for every fetch.
I have 10 cells/rows in a UITableView and I have set four of these cells to have some text like so:
if (indexPath.row == 0) {
cell.textLabel.text = #"Before School";
}
I'm doing all of this inside:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
I am trying to add a UITextField to only specific rows. How can I achieve this? I have managed to add a UITextField to either all or none of them using:
[cell addSubview:textField];
You should use if else statements. For example:
if([indexPath row] == 0){
[cell setAccessoryView:textField];
}else if([indexPath row] == 1){
[cell setAccessoryView:textField];
}
The process would be the same as setting the cell's textLabel.text property:
if (indexPath.row == 0)
{
[cell addSubview:textField];
}
Other examples:
Adds a UITextView to all even rows:
if (indexPath.row % 2 == 0)
{
[cell addSubview:textField];
}
See this SO post for more code: Having a UITextField in a UITableViewCell
I have a UITableView inside of a View created from a NIB. the table has one section containing 6 rows and 1 footer. when i use:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 80;
}
to return the height of the footer, which is being loaded from the same NIB. the UITableViewCell in the nib has a height of 80. the UITableViewCells are assigned using this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0){
if (indexPath.row == 0){
return companyCell;
}
if (indexPath.row == 1){
return firstNameCell;
}
if (indexPath.row == 2){
return lastNameCell;
}
if (indexPath.row == 3){
return emailCell;
}
if (indexPath.row == 4){
return passwordCell;
}
if (indexPath.row == 5){
return passwordConfirmCell;
}
}
return passwordConfirmCell;
}
i get a white line at the bottom of my table like the image at http://swnsn.com/white.png
if i return a footer height of 79, or 81, the white line goes away and the UITableViewCell scales to the width of the window
any thoughts?
I figured it out!! I was passing an instance of UITableViewCell to the footer. i have update my code to only pass a UIView and the problem has been resolved!
Have you tried more row manipulation like this in terms of it's height?
- (CGFloat)tableView:(UITableView *) theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
Question *question = [self.questions objectAtIndex:row];
NSString *theText = question.description;
UIFont *font = [UIFont fontWithName:#"Helvetica" size:14.0]; // You can choose a different font ofcourse.
int heightExtra = 0;
if (selectedIndexSegment == 0){
heightExtra = 54;
}
if (selectedIndexSegment == 1) {
heightExtra = 24;
}
CGSize withinsize = CGSizeMake(theTableView.frame.size.width -60, 1000); //1000 is just a large number. could be 500 as well
// You can choose a different Wrap Mode of your choice
CGSize sz = [theText sizeWithFont:font constrainedToSize:withinsize lineBreakMode:UILineBreakModeWordWrap];
return sz.height + heightExtra; // padding space of 20 should be fine!
}
And also how about
self.tableView.separatorColor = [UIColor clearColor];
Hope it helps...
Here is the deal: I have a UITableView with 2 sections, and I want to display a "no data" cell when the first section is empty, so that the 2 section headers are not stuck together (cause it looks weird).
It works great (even though I had trouble making it work at first, see this thread). I'm using viewForFooterInSection :
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if(section == 0)
{
if([firstSectionArray count] == 0)
return 40;
else
return 0;
}
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if(section == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
label.textAlignment = UITextAlignmentCenter;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
label.text = #"No row";
return [label autorelease];
}
return nil;
}
But the background color turns plain white when I display the section footer view. See image:
alt text http://img683.yfrog.com/img683/9480/uitableviewproblem.png
I like it better when the background is filled with empty cells. Does anyone have any idea how to do that? Thanks
The background is filled with empty cells when you have no footer. So do not implement a viewForFooterInSection (or titleForFooterInSection) method, and you will get the "empty cells" effect.
I'd recommend that you return a cell indicating that there are no entries to show, like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (matches.count>0) {
// Do your usual thing here
} else {
static NSString *cellId = #"noDataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor grayColor];
}
cell.textLabel.text = #"Aucun match";
return cell;
}
}
And of course, you would have to tell UIKit that you always have at least one cell in your section... I've added the isDeletingRow case that seems to trouble you (in comment).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) return matches.count>0 ? matches.count : (isDeletingRow ? 0 : 1);
// Set 'isDeletingRow' to YES when a delete is being committed to the table, in that case we let UIKit know that we indeed took care of the delete...
// And cover the other sections too...
}
When you are committing the edits, you need to set isDeletingRow for numberOfRowsInSection to return a satisfactory value...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
isDeletingRow = YES;
// Your current code when a row is deleted here...
isDeletingRow = NO;
if (matches.count==0) [self.tableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.5];
}
}