Only addSubView in certain cells - iphone

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

Related

Cells attached to each other in a UICollectionView

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:

Segmented Control with one tableview.How to remove the "Load More" cell which i added manually in the tableview?

I add a "Load More" cell in the tableview through check if the current cell is the last one.
but i have got 2 kinds of data to display,so i used a UISegmentedControl to switch.
once at the first tab of UISegmentedControl,the "Load More" cell appears at the last cell.but when i switch to second tab of the UISegmentedControl.The "Load More" cell still stays there...and there is still a "Load More" cell at the last of tableview.so there are 2 "Load More" cell shows.
i want to remove the first "Load More" cell,but i dont know how.Much THX.
EDIT:
static NSString * ID = #"TopicCustomCellIdentifier";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(IS_LAST_TOPIC_ROW){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
MTopicItem *topic;
cell.textLabel.textAlignment = NSTextAlignmentLeft;
if(SEGMENTINDEX == 0){
topic = [_my_topics_data_array objectAtIndex:indexPath.row];
}else{
topic = [_rec_topics_data_array objectAtIndex:indexPath.row];
}
[cell.textLabel setText:topic.name];
[cell.textLabel setFont:[UIFont systemFontOfSize:18]];
if(IS_RETINA){
[cell.imageView setImageWithURL:[NSURL URLWithString: topic.image]
placeholderImage:[UIImage imageNamed:#"default50"]];
}else{
[cell.imageView setImageWithURL:[NSURL URLWithString: topic.sImage]
placeholderImage:[UIImage imageNamed:#"default"]];
}
topic = nil;
return cell;
#define IS_LAST_TOPIC_ROW ((indexPath.row == [_my_topics_data_array count] - 1) && [_my_topics_data_array count] != 0) || ((indexPath.row == [_rec_topics_data_array count] - 1) && [_rec_topics_data_array count] != 0)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(SEGMENTINDEX == 0){
return [_my_topics_data_array count];
}else{
return [_rec_topics_data_array count];
}
}
Use the reloadData to refresh the view on segment touch.
[myTableView reloadData];
If you add a row manually, you can delete that manually too:
deleteRowsAtIndexPaths:withRowAnimation:
See here:
Check the last cell text label is load more don't add in second control.
This is happening due to the reusing of cell. When you deque a cell from the tableview check if it is not nil and reset content.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell) {
cell.textLabel.text=#"";
cell.imageView.image=nil;
}
Also cross check if IS_LAST_TOPIC_ROW is identifying the last row correctly.
Hope that helps.
keep a boolean to make sure when to show the cell or not.
Check it before you add the loadmore cell in cellForRowAtIndexpath
method .
Also the numberOfRowsInSection should be maintained with the same
bool.
When you click the loadmore cell set boolean to no and then reload table
How it looks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(showLoadMore)
{
if(IS_LAST_TOPIC_ROW){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(showLoadMore{
return [_my_topics_data_array count]+1;
}
else
{
return [_my_topics_data_array count];
}
}
just now,i suddenly got the answer.
I write 2 CONSTANT:
#define IS_LAST_MY_TOPIC_ROW (indexPath.row == [_my_topics_data_array count] - 1) && [_my_topics_data_array count] != 0
#define IS_LAST_REC_TOPIC_ROW (indexPath.row == [_rec_topics_data_array count] - 1) && [_rec_topics_data_array count] != 0
and in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if(SEGMENTINDEX == 0){
if(IS_LAST_MY_TOPIC_ROW){
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
topic = [_my_topics_data_array objectAtIndex:indexPath.row];
}else{
if(IS_LAST_REC_TOPIC_ROW){
cell.textLabel.text = #"加载更多";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
topic = [_rec_topics_data_array objectAtIndex:indexPath.row];
}

UITableView creates first row to last

I tried to make simple UIPopover with grouped table view. Everything is fine but first row is empty and it's data is in the last row. Whats wrong with my code?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
static NSString *kCellTextField_ID = #"CellTextField_ID";
cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID];
if (cell == nil) {
// a new cell needs to be created
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellTextField_ID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
// a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields)
UIView *viewToCheck = nil;
viewToCheck = [cell.contentView viewWithTag:kViewTag];
if (viewToCheck) [viewToCheck removeFromSuperview];
}
if (section == 1 && row == 0) {
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
} else {
UITextField *textField = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textField];
}
return cell;
}
I might have found the problem. Look at the following code.
if (section == 1 && row == 0) {
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
}
section == 1 indicates that you have two sections and your setting the first item of the second section to the first item of the array. That might be why it shows up last in your table view.
I finally figured out what was the problem. It was this sentence:
if (section == 1 && row == 0){
UITextView *textView = [[self.sisalto objectAtIndex: row] valueForKey:kViewKey];
[cell.contentView addSubview:textView];
}
It set just created cell with first value in datasource (sisalto). Obliviously, it should set first value of section 2 in datasource. When this is corrected everything works just fine.

UITableView Random White line in section footer when returning properly sized footer

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...

UITableView's background color becoming white when section is empty

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];
}
}