I'm just trying to test out the custom cell by have all the cells in the table say "Hi", but it won't work... the cells are all blank. It seems that the TheCell class is called before I can set the label text... I don't know how else to do it.
TheCell.h
#import <UIKit/UIKit.h>
#interface TheCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *arrivalLabel;
#end
TheCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.arrivalLabel.frame = CGRectMake(0, 0, 320, 30);
[self.contentView addSubview:self.arrivalLabel];
}
return self;
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"anIdentifier";
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
cell.arrivalLabel.text = #"hi";
return cell;
}
You have a few issues. You are calling the init method of your cell class. You should be calling the initWithStyle:reuseIdentifier: method instead.
You should also only allocate the cell if the call to dequeue... returns nil.
Then you need to make sure that your cell's initWithStyle:reuseIdentifier: method is creating the label instance for the arrivalLabel property.
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.arrivalLabel.text = #"hi";
The other big problem is in your cell's initWithStyle:reuseIdentifier: method. You never create the label.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_arrivalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[self.contentView addSubview:_arrivalLabel];
}
return self;
}
This is not correct:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
You have to do something like:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Related
it says
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
is there any mistake if i implement UITableView cell inside custom UITableViewCell ?
So first in MainViewController, Using MainView that have UITableView,
in the MainViewController, especially in ViewDidLoad, i register self.mainView.tableView.delegate = self; also self.mainView.dataSource = self;
and in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath i have this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.filteredCategoriesAllListLocalDataArray count] > 0) {
static NSString *cellID = #"ChildTableViewCell";
ChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(nil == cell) {
cell = [[ChildTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellID];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
CategoryModel *currentCategory = [self.filteredCategoriesAllListLocalDataArray objectAtIndex:indexPath.section];
ChildModel *currentChildData = [currentCategory.childModelArray objectAtIndex:indexPath.row];
NSLog(#"================== %#", currentChildData.nameString);
cell.childData = currentChildData;
[cell initializeChildTableView];
cell.clipsToBounds = YES;
return cell;
}
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
and then in ChildTableViewCell i have this:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth([UIScreen
mainScreen].bounds), CGRectGetHeight(self.contentView.frame))style:UITableViewStyleGrouped];
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.showsHorizontalScrollIndicator = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.scrollEnabled = NO;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.contentView addSubview:self.tableView];
}
return self;
}
but in step 4, got this error message:
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
im Using UITableView inside UItableViewCell cause want to make UITAbleView Tree Level with custom data, i've been search all reference for UITableView Multi level but it doesn't help at all.
I'm trying to display a custom UISearchBar Results Cell. I created a custom UITableViewCell to display the information but I want to customize the UISearchBar results.
I have this code in my class:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PodsCell";
LinhaPod *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[LinhaPod alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (isSearching && [self.searchResults count])
{
NSString *texto = [self.searchResults objectAtIndex:indexPath.row];
NSArray *arrTexto = [texto componentsSeparatedByString:#"#"];
cell.titulo.text = arrTexto[6];
cell.dataPod.text = [NSString stringWithFormat:#"%# - %#", arrTexto[1], [arrTexto[3] stringByReplacingOccurrencesOfString:#"." withString:#"'"]];
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
This code is not working. I tested with "cell.textLabel.text = #"test";" and worked.
I'm using Search Bar and Search Display Controller.
This is my UITableViewCell
Have you added the UILabels to your UITableViewCell ? Your initWithStyle in LinhaPod.m should look like this snippet below. (Assuming your are not using a xib file for this custom cell)
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_dataPod = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
[self addSubview:_dataPod];
_titulo = [[UILabel alloc]initWithFrame:CGRectMake(160, 0, 160, 44)];
[self addSubview:_titulo];
}
return self;
}
Of course, in your LinhaPod.h you would also have
#interface LinhaPod : UITableViewCell
#property(nonatomic) UILabel *titulo;
#property(nonatomic) UILabel *dataPod;
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[MCSelectCategoryTableViewCell_iPhone class] forCellReuseIdentifier:#"selectCategoryCell"];
}
If you want to use a custom cell in the tableView of the searchDisplayController you could register any of the UITableView subclasses of your storyBoard by registering it like this in the searchDisplayController.searchDisplayTableView.
I am using grouped UITableview with custom cell.My tableview consists of two sections.I need to change textfield frame only for two rows in section zero.How to possible??Please help me.Go through my code
customcell.m
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textfiled1 = [[UITextField alloc]init];
self.textfiled1.returnKeyType = UIReturnKeyNext;
textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.contentView addSubview:self.textfiled1];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textfiled1.frame = CGRectMake(50, 3, 250,40);
}
#pragma Tableview Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
static NSString *CellIdentifier = #"Cell";
customcell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[customcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if (indexPath.row==0) {
cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame
cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100);
}
else if(indexPath.row==1)
{
cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame
}
else if(indexPath.row==2)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
}
else if(indexPath.row==3)
{
}
return cell;
}
else if(indexPath.section == 1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
cell.textLabel.text = #“Section1";
return cell;
}
}
Insted of this make a property so that u can set the specified frame u want hear the code, in controller u need to set frame for each cell see the code below
//CustomCell.h file
#interface CustomCell : UITableViewCell
#property(nonatomic,assign) CGRect TextFieldFrame;//put this to change the frame
#property (nonatomic,retain)UITextField *textfiled1;
#end
//in CustomCell.m file
#import "CustomCell.h"
#implementation CustomCell
#synthesize TextFieldFrame;//sysnthesise
#synthesize textfiled1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.textfiled1 = [[UITextField alloc]init];
self.textfiled1.backgroundColor = [UIColor greenColor];
self.textfiled1.returnKeyType = UIReturnKeyNext;
textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.contentView addSubview:self.textfiled1];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textfiled1.frame = CGRectMake(self.bounds.origin.x + self.TextFieldFrame.origin.x, self.bounds.origin.y + self.TextFieldFrame.origin.y, self.TextFieldFrame.size.width, self.TextFieldFrame.size.height);
// self.textfiled1.frame = CGRectMake(50, 3, 250,40);//hear u are setting the contant frame thats wy frames are not changed
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//in ViewController.m file
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
#implementation ViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
static NSString *CellIdentifier = #"Cell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if (indexPath.row==0) {
cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, u can set the frame for each cell hear
cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100);
}
else if(indexPath.row==1)
{
cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, heare also
}
return cell;
}
else if (indexPath.section == 1)
{
static NSString *CellIdentifier = #"Cell_2";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if(indexPath.row==0)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
cell.TextFieldFrame = CGRectMake(80, 3, 180, 40); //for other cells default frame u need to set it heare
}
else if(indexPath.row==1)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
cell.TextFieldFrame = CGRectMake(80, 3, 180, 40);
}
return cell;
}
else
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
Create a custom table view cell initializer and pass indexpath . So now in tableview cell you know the row, based on row number make the changes
I know this is simple, but I can't find a simple answer! I have a regular UITableView and I just added a text view to a custom cell. I'm not sure how to access this textview in cellForRowAtIndexPath. I tried making it an iboutlet, but it doesn't show up when I do cell.??!?!?!?. The reuse is set also. Any ideas?
Edit: I also did all the connections in ib
In the .h file...
#property(nonatomic, strong) IBOutlet UITextView *myTextView;
In the .m file...
#synthesize myTextView = _myTextView;
static NSString *CellIdentifier = #"all_table_reuse_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"%s", __PRETTY_FUNCTION__);
}
The problem is I should be able to do cell.myTextView, but it's not there
For Example
static NSString *cellIdentifier= #"Cell";
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell=[[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil]objectAtIndex:0];
}
cell.myTextView.text=#"your text";
Do like this,
YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell. myTextView.text = #"My TextView";
return cell;
May be this is what you want.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Why so serious?";
return cell;
Edit: For custom cell
In .h file
#import <UIKit/UIKit.h>
#interface TestCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *tv;
#end
In .m
#import "TestCell.h"
#implementation TestCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void) setFrame:(CGRect)frame
{
float inset = 20.0f;
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end
In CellForRowAtIndexPath
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tv.text = #"This is textview";
return cell;
Hope this helps... :)
I am trying to subclass a UITableViewCell for my table but the cell keeps on returning nil. Anyone know why? Thanks.
TableView Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(#"Nil = YES");
}
cell.callType = currentCall.currentCallType;
return cell;
}
SubClass Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
UIView *myContentView = self.contentView;
label.text = callType;
label.textColor = [UIColor blackColor];
[label addSubview:myContentView];
}
return self;
}