The invention rela
A randomized contr
It was after the l
The most iconic im
Q:
How to use Htm
Q:
What is the pu
LONDON—The biggest
Q:
How does a com
Q:
Why do I get t
Novel N-terminal aQ:
How to get text color from a cell in a custom cell class - Xamarin iOS
I have a custom cell which has a label property in it.
I use the custom cell class to add this label to any other tableview's cell.
What I want to do is get the text color of this label so that I can change the cell's textColor to that of the text color in this label.
My Custom Cell Class :
public class CustomCell : UITableViewCell
{
public static UILabel myLabel;
public CustomCell(IntPtr handle) : base(handle)
{
}
public static void setCustomLabel(UITableViewCell cell, UILabel textLabel)
{
CustomCell myCell = (CustomCell)cell;
myCell.myLabel = textLabel;
}
[Export("initWithStyle:reuseIdentifier:")]
public CustomCell(UITableViewCellStyle style, string reuseIdentifier) : base(UITableViewCellStyle.Subtitle,reuseIdentifier)
{
myLabel = new UILabel();
ContentView.AddSubview(myLabel);
}
[Export("initWithCoder:")]
public CustomCell(NSCoder aDecoder) : base(UITableViewCellStyle.Subtitle, aDecoder)
{
myLabel = new UILabel();
}
[Export("setText:")]
public override void SetText(UITextField value)
{
myLabel.Text = value.Text;
}
[Export("setDetailText:")]
public override void SetDetailText(NSString value)
{
myLabel.Text = value;
}
[Export("setDetailTextColor:")]
public override void SetDetailTextColor(UIColor value)
{
myLabel.TextColor = value;
}
}
In my viewController class I did :
private void setUpTableView()
{
...
private TableSource.CustomCell objCustomCell;
objCustomCell = (TableSource.CustomCell)myTableView.DequeueReusableCell("mycell");
objCustomCell.myLabel.Text = "MyText";
// this next line gives error "Reference to non-static field requires an object reference."
objCustomCell.SetTextColor(objCustomCell.myLabel.TextColor);
// objCustomCell.myLabel is not declared. Do I have to create a UILabel instance here?
// objCustomCell.myLabel = new UILabel()
objCustomCell.Subviews.Clear();
objCustomCell.Subviews.Add(new UILabel()
{
Text = "Hello World!"
});
myTableView.Source = objCustomCell;
}
I want the cell to show the text and text color of the text which I added using myLabel and the textColor property of it. How do I accomplish this?
The error I get is :
"Reference to non-static field requires an object reference."
A:
I found the answer to my own question.
The reason why I wasn't able to add the text color by adding a UILabel to the cell was because the UILabel was being added inside the static myLabel property and it couldn't be seen outside of that property.
I had to create a label object and then set the text color of that object and then add it to the cell.
The corrected code looks something like this :
objCustomCell.myLabel.Text = "MyText";
objCustomCell.myLabel.TextColor = objCustomCell.myLabel.TextColor;
objCustomCell.Subviews.Clear();
objCustomCell.Subviews.Add(new UILabel()
{
Text = "Hello World!"
});
myTableView.Source = objCustomCell;
Hope this helps someone in the future :)
A:
Your cell definition has no name. Do not do that; you will never have any control over the label inside the cell, and cannot change the color anyways.
Instead, change your setTextColor to something like this:
public void SetTextColor(UIColor value)
{
if(myLabel == null)
return;
// set the color of the label in the view hierarchy
myLabel.TextColor = value;
}
// then use it like this:
myCustomCell.SetTextColor(myCustomCell.myLabel.TextColor);
If you are adding and removing the UILabel inside your cell, then you can do something like this:
public void SetTextColor(UIColor value)
{
if(myLabel == null)
return;
// set the color of the label in the view hierarchy
myLabel.TextColor = value;
// force the label to draw itself to the cell, reusing it
myLabel.SetNeedsDisplay();
}
// then use it like this:
myCustomCell.SetTextColor(myCustomCell.myLabel.TextColor);
I would recommend you also add code to change the color of the UITableViewCellAccessoryIndicatorView that has the colored chevron. That can be done in SetTextColor as well.
A:
If you want to change color of all cells in the table view without using CustomCell class , then you can use custom UITableViewCell. Create two UILabel in custom cell. Place one label below other and set cell background to your desired color.
CustomTableCell.h
@interface CustomTableCell : UITableViewCell {
UILabel *label1;
UILabel *label2;
}
@property (nonatomic, retain) UILabel *label1;
@property (nonatomic, retain) UILabel *label2;
@end
CustomTableCell.m
- (void)awakeFromNib {
self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
[self.contentView addSubview:self.label1];
self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 60, 30)];
[self.contentView addSubview:self.label2];
[self.label1 setTextColor:[UIColor grayColor]];
[self.label1 setTextAlignment:UITextAlignmentCenter];
[self.label1 setBackgroundColor:[UIColor whiteColor]];
[self.label2 setTextAlignment:UITextAlignmentCenter];
[self.label2 setBackgroundColor:[UIColor whiteColor]];
}
You can change label1 color by setting color in this method.
-(void)setLabelTextColor:(UIColor *)color
{
label1.textColor = color;
label2.textColor = color;
}
And use this method for changing all label text color.
CustomTableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setLabelTextColor:[UIColor greenColor]];
If you are adding tableView and tableViewCell programmatically, please use the following code for that.
CustomTableCell *cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
Hope, it helps you to get out of this error.