I am trying to reduce the cell's one label's width at particular indexPath.row but its not working..
below i added code for that and I am using iOs 5 storyboard.
Any one have other way to change the size of label at run time ??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"name&email"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
UILabel *detail = (UILabel *)[cell viewWithTag:101];
if(indexPath.row == 0){
nameLabel.text = #"Name : ";
detail.text = #"";
}else if(indexPath.row == 1){
nameLabel.text = #"Email : ";
detail.text = #"";
}else if(indexPath.row == 2){
nameLabel.text = #"Location Alerts";
detail.text = #"Notifies people when they are close to a saved spot";
// here i am changing the width of lable
detail.frame = CGRectMake(0, 0, 100, 0);
}else if(indexPath.row == 3){
nameLabel.text = #"Share This App :";
detail.text = #"(post to facebook/twitter)";
}else if(indexPath.row == 4){
nameLabel.text = #"Review This App :";
detail.text = #"(on iTunes)";
}else if(indexPath.row == 5){
nameLabel.text = #"Get Help :";
detail.text = #"(send an email to me)";
}
// Configure the cell...
return cell;
}
I'm not an expert, but I'm seeing that you are using two differents identifier.
If I were you I would try:
static NSString *CellIdentifier = #"name&email";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"name&email"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ok? Tell me if it works :)
If you have the custom cell created in Storyboard, check what the Identifier is and use that. The Identifier should be under the Attributes Inspector. If the Identifier is Cell, then your code should be something like this to get the Cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
If you have a UILabel called detail which you want to change the width of, you can change the size by changing the frame and reusing the previous frame origin and size height.
detail.frame = CGRectMake(detail.frame.origin.x, detail.frame.origin.y, newWidth,detail.frame.size.height);
Make sure you pass detail.size.height as the height, to keep the height the same (I noticed in your example that the height is 0 which wouldn't make it visible). Hope that helps
Related
I am trying to hide the grey line (seperator) under two cells in my UITableView however I am having trouble.
I have a Grouped UITableView and this is my cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (indexPath.section == 0)
{
if (indexPath.row == 0) {
// Date
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Date:";
cell.detailTextLabel.text = currentDateString;
} else if (indexPath.row == 1) {
// UIPicker
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.clipsToBounds = YES;
[cell.contentView addSubview:datePickerForCell];
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
// Order No.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Order No:";
cell.detailTextLabel.text = #"001-01989";
} else if (indexPath.row == 2) {
// Location
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Completed:";
CGFloat optionalRightMargin = 10.0;
CGFloat optionalBottomMargin = 10.0;
completedByTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, cell.contentView.frame.size.width - 110 - optionalRightMargin, cell.contentView.frame.size.height - 10 - optionalBottomMargin)];
self.completedByTextField.delegate = self;
completedByTextField.textColor = [UIColor grayColor];
completedByTextField.tag = 2;
completedByTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
completedByTextField.textAlignment = NSTextAlignmentRight;
completedByTextField.placeholder = #"Name";
completedByTextField.text = completedByString;
[cell.contentView addSubview:completedByTextField];
} else if (indexPath.row == 3) {
// Hours Worked
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Hours Worked:";
cell.detailTextLabel.text = #"4 hrs 26 min";
}
} else if (indexPath.section == 2) {
if (indexPath.row == 0) {
// Description
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Description:";
} else if (indexPath.row == 1) {
// Description Textfield
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
[cell.contentView addSubview:descriptionTextView];
}
} else if (indexPath.section == 3) {
if (indexPath.row == 0) {
// Materials
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Materials:";
}
}
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
return cell;
}
I would like to hide the separators under the
// Description Textfield Cell
and
// Materials Textfield Cell (this has not been written yet, its just I wanted to make clear that there will be two cells I want to do this two not just one as it may affect your answer I guess...)
I have read about using tableview.separatorcolor but this changes the color of every cells separator color in the TableView.
As far as I know its not possible to just hide cell separator for some cells. It will be applicable for complete table view.
However you can achieve this by adding a transparent or white image view on those cells which you want to hide the separator. Keep the Image view size a little bit larger than the row height. May be it should help.
Otherwise in general its not possible to just hide cell separator for some cells. If you make any changes in a particular separator, it will be reflected in all table view cell separators.
You can't trigger the separator to hide/unhide for specific cells for a UITableView. Rather, disable the cell separator altogether and add a custom separator to the cells you need. Add the following code to the cells you need to show a separator on:
UIView* separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, cell.contentView.frame.size.width, 1)];
separatorView.backgroundColor = [UIColor lightGrayColor]; // set custom color here
[cell.contentView addSubview:separatorView];
You can use this line of code in your cellForRowAtIndexPath method-
tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
you can hide the line by set tableview offset.
cell.separatorInset = UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);
I'm trying to create a standings table with a UITableView.
I've created a custom cell, with 7 labels. I have used grouped cells to differentiate between AL Central, NL East, etc.
Check out this image, when I load the view:
Compare that with the image when I scroll the view:
Here is my code (removed the repeated code):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}// end
//rank
UILabel *rank = (UILabel *)[cell viewWithTag:101];
//team
UILabel *team = (UILabel *)[cell viewWithTag:102];
if(indexPath.row == 0)
{
NSArray *division = #[#"AL Central", #"AL East", #"AL West", #"NL Central", #"NL East", #"NL West"];
rank.text = #"";
team.text = division[indexPath.section];
cell.backgroundColor = textPrimary;
team.textColor = backgroundPrimary;
team.font = [UIFont boldSystemFontOfSize:5.0f];
won.font = [UIFont boldSystemFontOfSize:11.0f];
return cell;
}
cell.backgroundColor = [UIColor clearColor];
rank.textColor = textPrimary;
team.textColor = textPrimary;
int new_row = indexPath.row + indexPath.section*5 - 1;
rank.text = [[[dataSource objectAtIndex:new_row] objectForKey:#"rank"] stringValue];
team.text = [[dataSource objectAtIndex:new_row] objectForKey:#"first_name"];
return cell;
}
I solved this problem by setting the font again when it wasn't the header:
team.font = [UIFont systemFontOfSize:5.0f];
I set uitableview's cell as following
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
[cell addSubview:bigphoto];
}
else {
cell.backgroundColor = [UIColor blackColor];
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 320;
}
return 220;
}
I want the first cell(indexPath.row = 0) is different from others, the first cell is set a bigphoto and its background color is red(with its UI Design), others are myphoto.png with black color background(with the UI Desigin of DetailCell), but the code runs with the wrong result,
the first cell(indexPath.row = 0) is right, but the indexPath.row = 3 or 6 or 9.. are the same as indexPath.row = 0, not like indexPath.row = 1 or 2 or 4...
so how can I fixed this? thanks
The cell is being reused, you should use different cell identifiers for cells with different representation. This should work:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
DetailCell *cell = nil;
if (indexPath.row == 0) {
cellIdentifier = #"Cell0";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bighoto.tag = 1;
[cell addSubview:bigphoto];
}
UIImageView *bigphoto = (UIImageView *)[cell viewWithTag:1];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
}
else {
cellIdentifier = #"Cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor blackColor];
}
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
This is because the first cell is "reused." Since you are using these cells for two separate purposes, it would be much cleaner to have a prototype cell in the storyboard that you use for just the first row, and then otherwise reuse cells for the rest of the rows.
In your case best option is to use the static tabelViewCells. Please check on this link.
I am trying to delete a cell from the table from the table view when no data is returned from Parse. But no matter what I try I can't find any solution to keep from having a blank cell (Cell when no data is passed) ahead of the cell with content.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
static NSString *simpleTableIdentifier = #"RecipeCell";
NSString *identifier;
if (indexPath.row == 0) {
identifier = #"Cell";
} else if (indexPath.row == 1) {
identifier = #"Cell2";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// Configure the cell
if (![#"test" isEqual: [object objectForKey:#"teacher"]]){
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = #"";
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = #"";
return cell;
} else {
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey:#"message"];
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = [object objectForKey:#"teacher"];
return cell;
}
}
Thanks for any help given!
You cannot control the number of cells in a UITableView from the cellForRowAtIndexPath method - You need to return the correct value from the numberOfRowsInSection method. Refer to the UITableViewDataSource protocol reference and the Table View Programming Guide
Addtionally, this code block -
if (indexPath.row == 0) {
identifier = #"Cell";
} else if (indexPath.row == 1) {
identifier = #"Cell2";
}
is somewhat redundant - all of your cells are UITableViewCellStyleDefault, so they are interchangeable. Using two different identifiers is not necessary - If you intend to use different custom cell types in the future then you can keep this concept, but you probably wouldn't use the indexPath.row directly, rather you would be driven by the data type in your model for the row in question.
In my UITableView I am trying to display all the items in my plist but its not showing all the items. Actually it is showing most of it but the lower items are being repeated for some odd reason. I basically want to show all the keys in the plist with their respective values. Is the list too long to display? there's about 30 items.
First I tried to sort the keys and thought that was the problem, so then I didn't sort at all and I get the same problem, lower down the list the items get repeated and not showing the last 3 items. Is there a limit?
Below is some code, I've just modified to fit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
What I've found is that if I don't use the labels and just go for the generic cell.textLabel.text approach then all the items are displayed correctly. However if I use the UILabel approach, the bottom items are not shown. I need to go this route as I'm adding other items in the Cell.
Working Code.
Initialization and creation of cell must be created first, then using that referenced cell remove from superview, then render the subviews. So reordering of the code from above.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
// Create/Initialize Cell first
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
}
}
// remove from superview
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// render the subviews
if (indexPath.section == 0 || indexPath.section == 2) {
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
} else if(indexPath.section == 1) {
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
It looks like cells are being reused and you are just adding new views to their existing contents. You need to reset the content, as described here: UITbleViewCell Class Reference. If you were just setting the cell's textLabel each time, setting a new value would suffice here, but if you are adding subviews you may need something more like [cell.contentView.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
The Limit for a Tableview is the free RAM size.
Please post some Code. But i think that this could be a Problem with Cell caching.