I want to resize the default textLabel of a UITableViewCell because I display a image at the right of the rows. I Tryed with this code but it doesn't works, and I don't understand why.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
cell.textLabel.frame = CGRectMake(5, 5, 100, 50);
//...
}
u should used custom label
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *Lbl = [[UILabel alloc]initWithFrame:CGRectMake(75.0f, 4.5f, 360.0f, 20.0f)];
[cell.contentView addSubview:Lbl];
[Lbl release];
}
I think that is impossible.
Make custom cell.
UILabel myTextLabel;
//Set Frame and do something.
[cell.contentView addSubview:myTextLabel];
textLabel is readonly property so we can't set frame..
#property(nonatomic,readonly,retain) UILabel *textLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. label will be created if necessary.
#property(nonatomic,readonly,retain) UILabel *detailTextLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. label will be created if necessary (and the current style supports a detail label).
use custom cell ...
You can not change a cell's textLabel's frame except or you go with custom cell and use UILabel, UIImageView as a subview of the cell.
Related
I have a customCell in StoryBoard "CategoryCell", I have UIImageView with the cell which is also part of the cell also tied up in StoryBoard. The UIImageView is filled with a plain yellow color. I intend to add a label to this plain yellow image, the label varies depending on the cell.
The code below works fine initially, but when I scroll the the tableView, I see label in the image getting messed up, it's like its trying to write new text in top of the text. I figured it's hitting cellForRow again and it's adding new label, How can I make it not create new label on top of old one?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableView isEqual:wordsTableView])
{
CategoryCell *cell = [CategoryTableView dequeueReusableCellWithIdentifier:#"CategoryCellIdentifier" forIndexPath:indexPath];
if(!cell)
cell = [[CategoryCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"CategoryCellIdentifier"];
NSString *text = [[theCategories objectAtIndex:indexPath.row] uppercaseString];
//Add label now
catLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
catLabel.text = text;
[cell.codeImageView addSubview:catLabel];
return cell;
}
You could give the label a tag and use that to check to see if it exists before creating it again :
UILabel *catLabel = [cell.codeImageView viewWithTag:100];
if (nil == catLabel) {
catLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
catLabel.tag = 100;
[cell.codeImageView addSubview:catLabel];
}
catLabel.text = text;
Though if it gets any more complicated I might look at subclassing UITableViewCell and using a xib to instantiate the label.
I am using a simple UITableView and using separator as a image. When table is loaded initially it looks fine but when user scrolls up and leaves, the separator image disappears. Again when user scrolls down and leaves, lines appears again.
Please let me know how to resolve this.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellID=#"Cell"
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SwitchCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageview *aSwitch = [[UIImageview alloc] initWithImage:[UIImage imageNamed:#"divider.png"]];
separator.frame = CGRectMake(0,50,320,1);
[cell.contentView addSubview:seperator];
}
..........
It's not clear where you declare "separator" but you'll want to make sure you initialize and allocate it as a separate instance for each cell.
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 1)];
[cell.contentView addSubView:separator];
Are you trying to use one separator instance for all separators in the table? That won't work because a view can only be added to a single superview. You should initialize a new separator for each cell in your reuse pool.
I am adding label to uiimage and then adding that image on uitable view. but it is not showing. Can anyone help please?
This is code i am using:
- (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 setSelectionStyle:UITableViewCellSelectionStyleNone];
}
//image which contains label
UIImageView *BgImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell-bg.png"]];
BgImage.frame=cell.frame;
//label which will be on image
UILabel *NameLAbel=[[UILabel alloc]initWithFrame:CGRectMake(25, 5, 45, 45)];
NameLAbel.textColor=[UIColor blackColor];
if (indexPath.row==0) {
NameLAbel.text=#"Home";
}
else if (indexPath.row==1) {
NameLAbel.text=#"You";
}
else if (indexPath.row==2) {
NameLAbel.text=#"Contacts";
}
else if (indexPath.row==3) {
NameLAbel.text=#"Settings";
}
else {
NameLAbel.text=#"Sign Out";
}
NSLog(#"%#",NameLAbel.text);
[BgImage addSubview:NameLAbel];
//adding image on cell
[cell.contentView addSubview:BgImage];
[NameLAbel release];
[BgImage release];
return cell;
}
I'm not sure if you can actually add a UILabel to an UIImageView, although they are both UIView subclasses, I think the imageview will only display the image.
try adding the label to cell.contentView after you've added the image to the view. That should achieve the affect you are after.
Either that, or create of UIView which will hold both the UIImageView and the UILabel
Adding some UI widget as a subview of a UIImageView sounds really questionable. Even if it works now it may not work in the future, and I believe this breaks the UIKit convention. If you want to make one view look like it is "part of" another view, place the two as siblings, either directly in the cell.contentView as Amit noted, or if you want clipping so that the label looked like it was being clipped by the image, create a UIView container, enable the "clip subviews" setting of that view, and place the UIImageView and UILabel as siblings within that container UIView.
Might be a stupid question.
I have a UITableview, with multiple cells. In each cell I am displaying some data. I am not using cell's text property to display data. Instead I have a custom label inside my cell, which is displaying the text.
My question is:
When I click on the cell, I need to retrieve the data from the cell. How can I do this.
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *CellTextlabel = [[UILabel alloc] init];
CellTextlabel.tag = 222;
[CellTextlabel setFrame:CGRectMake(40, 5, 200, 20)];
[cell.contentView addSubview:CellTextlabel];
[CellTextlabel release];
}
UILabel *editCellTextlabel = (UILabel *)[cell.contentView viewWithTag:222];
editCellTextlabel.font = [UIFont boldSystemFontOfSize:18];
editCellTextlabel.text = contact.lastName;
In your didSelectRowAtIndexPath method, you may do it as follows:
UITableViewCell *cell = (UITableViewCell *)[self.tableViecellForRowAtIndexPath:indexPath];
UILabel *textLabel = (UILabel *)[cell viewWithTag:222];
Now you can retrieve the data in the cell's UILabel using textLabel.text
You could get access to that label in didSelectRowAtIndexPath: with
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *myLabel = [cell.contentView viewWithTag:222];
But probably worth asking, why are you adding a sub label instead of using the textLabel property? you can modify its frame, settings etc and then you don't have to worry about tags, since this property is exposed in UITableViewCell by default
In the -tableView:didSelectRowAtIndexPath: method, you can get the data from your tableView's array:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id objectForCell = [self.myArray objectAtIndex:indexPath.row];
//do what you want with the above data.
}
I want to display a label showing a number in each cell of the tableview but the label is only visible when I click on a row (when the cell is highlited)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
label = [[UILabel alloc] initWithFrame:CGRectMake(200,10, 15, 15)];
label.tag = 1;
[cell.contentView addSubview:label];
[label release];
}
else {
label = (UILabel *)[cell viewWithTag:1];
}
if (indexPath.row == 0) {
cell.textLabel.text = #"Photos";
label.text = [NSString stringWithFormat:#"%d",1];
}
return cell;
}
I had the same problem and it was solved by setting the text for textlabel BEFORE adding the custom label as a subview.
...
cell.textLabel.text = #"X";
...
[cell.contentView addSubview:label]
When you update the textLabel property of a UITableViewCell, it lazily creates a UILabel and adds it to the cell's subviews. Usually you wouldn't use a combination of textLabel and adding subviews to contentView, but if you do you need to make sure the textLabel view isn't placed over the top of your contentView subviews.
First, I assume this is targeting 3.0. Apple has changed how UITableViewCells are created in 3.0, and you should move over to that. -initWithFrame:reuseIdentifier: is deprecated.
That said, a likely problem is that the built-in textLabel is interfering with your added label, perhaps overlapping. You should look first at whether one of the new built-in styles meets your needs directly. If not I would recommend either just using your own views or only using the built-in views, possibly rearranging them. If you want to rearrange them, Apple suggests subclassing the cell and overloading -layoutSubviews. I also believe that -tableView:willDisplayCell:forRowAtIndexPath: is a good place to do final cell layout without subclassing.
Using a custom UITableViewCell gives you more control over the layout of a cell. Add custom views to the cell's contentView in the subclass and override the layoutSubviews to set the order of the subviews:
- (void)layoutSubviews {
[super layoutSubviews];
[self.contentView bringSubviewToFront:self.yourCustomView];
}