cannot register delegate and data source of UITableView inside UITableViewCell - ios

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.

Related

iOS cell accessoryView set the same view stuck

The source codeļ¼š
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
- (UITableView *)tableView
{
if (!_tableView) {
CGRect frame = self.view.bounds;
frame.size.height -= (64 + 40);
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.separatorColor = __color_ListSepatator;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
}
return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45.0f;
}
/*Here is dalegate*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string_id = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string_id];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string_id];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
/*Here is jammed*/
cell.accessoryView = self.viewccessory;
return cell;
}
/*Here is propety*/
- (UIView *)viewccessory
{
if (!_viewccessory) {
_viewccessory = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
_viewccessory.backgroundColor = [UIColor orangeColor];
}
return _viewccessory;
}
Question
When different cell repeat the same accessoryView Settings, getting stuck when I was out of the controller, can't into the interface
Each time when the controller card dead don't know why.Why can't accessoryView set the same view?Ask everybody to help. Thanks
accessoryView for each tableview cell should be independent but not shared, you should make a new view very time when assigning to table view cell.
Why acessoryView can't be shared? Each view should only has one superview.

Set Value via Custom Table Cell

In the following custom table cell (StepperProgressTableViewCell), I have hardcoded currentSection to 2 for testing, but when it hits StepperProgressTableViewCell awakeFromNib method, it shows 0.
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperTableCellDelegate = self;
cell.currentSection = 2;
return cell;
}
StepperProgressTableViewCell.m
#implementation StepperProgressTableViewCell
#synthesize stepperView, currentSection;
- (void)awakeFromNib {
[super awakeFromNib];
[self setUpViews];
// it always shows 0
NSLog(#"%lu", currentSection);
}
- (void)setUpViews {
self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:#[NSLocalizedString(#"Processing", nil), NSLocalizedString(#"Ready", nil), NSLocalizedString(#"Delivered", nil)]];
self.stepperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.stepperView.userInteractionEnabled = YES;
self.stepperView.currentSection = currentSection
[self addSubview:self.stepperView];
}
AYStepperView.m
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
// ultimate goal is to set AYStepperView's currentSection
// the following also prints 0
NSLog(#"%lu", _currentSection);
return self;
}
You need add a method to change this property and in the implementation pass the value to your stepperView
StepperProgressTableViewCell.h
add a method like
-(void)setupCurrentSection:(int)newCurrentSection;
StepperProgressTableViewCell.m
-(void)setupCurrentSection:(int)newCurrentSection{
self.currentSection = newCurrentSection
self.stepperView.currentSection = newCurrentSection
}
in your cellForRow then add this line
[cell setupCurrentSection:2];
Hope this helps

Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:6509

iOS 7:
Exception Detail: *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:6509
Code:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UISIZE.width, UISIZE.height -64) style:UITableViewStylePlain];
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] init];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GWVipRechargeCell class]) bundle:nil] forCellReuseIdentifier:#"GWVipRechargeCell"];
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
GWVipRechargeCell * cell = [tableView dequeueReusableCellWithIdentifier:#"GWVipRechargeCell"];
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:#"GWVipRechargeCell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I found Exception breakpoint jump at self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
some sets of tabelview :
self.tabelView.dataSource = self;
self.tabelView.delegate = self;
self.tabelView.tableFooterView = [[UIView alloc] init];
self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
I try all answers of iOS 5 Storyboard Custom Cell Crash: UITableView dataSource must return a cell ,but it still crash at self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
Solution:
i move the settings order
self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tabelView.tableFooterView = [[UIView alloc] init];
and it Run successed! i'm confused too!!!
if I set tableHeaderView also have this question
[self.YOURTABLEVIEW registerNib:[UINib nibWithNibName:#"YOURCUSTOMCELL" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"YOURCUSTOMCELL_IDENTIFIER"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YOURTABLEVIEWCELL_CLASS *cell = [tableView dequeueReusableCellWithIdentifier:#"YOURTABLEVIEWCELL_IDENTIFIER"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"YOURTABLEVIEWCELL_IDENTIFIER" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// here write your cell coding like give value to your lable, image , etc.
return cell;
}
Try this -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
GWVipRechargeCell *cell = [tableView dequeueReusableCellWithIdentifier:#"GWVipRechargeCell"];
if (!cell) {
// if cell is empty create the cell
cell = [[GWVipRechargeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GWVipRechargeCell"];
}
return cell;
}
set the tableView other properties before set tableHeaderView or tableFooterView like this:
// set tableView's other properties
tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
// set tableHeaderView or tableFooterView after set tableView's other properties
tabelView.tableHeaderView = headView;
tabelView.tableFooterView = footerView;

The UITableViewCell's width and height always is 320 and 44

I'm sorry for my shoddy English.
A cell named 'A' contains a subTableView;The 'A' is normal,but the subCell's frame.size always is (320,44).
show the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellID = #"OrderViewControllerCellID";
OrderTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (nil == cell) {
cell = [[OrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
OrderCellModel* model = self.orderDataSourceArray[indexPath.row];
cell.model = model;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
'A'cell contentView
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_productsArray = [[NSMutableArray alloc] init];
_productsTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
_productsTableView.delegate = self;
_productsTableView.dataSource = self;
[self.contentView addSubview:_productsTableView];
_productsTableView.scrollEnabled = NO;
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellID = #"OrderTableViewSubCellID";
OrderSubTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[OrderSubTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
OrderProductModel* model = _productsArray[indexPath.row];
cell.model = model;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _productsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return transForHeight(86.f);
}
thank you for looking at my question.
I'm sorry for my little English again.
initWithStyle allways returns cells with height of 44 unless you overwrite it in a subclass. You should change height in delegate methods.
default cell height is 44.0. You need to override heightforrowatindexpath method.

UITableView for iOS

I am very new to Objective-C. I just searched and tried to learn about UITableView.
I have created this UITableView. instead of "a" in all the rows i want it to be in a sequence like "a b c d..." and if i increase the no of rows it should scroll. its not scrolling so here is my code.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
cell.textLabel.text = #"a";
return cell;
}
#end
set table view Delegates in IB or if u created Programmatically then in viewDidLoad Take one array fill it Dynamically or Statically.Give array count in NumberOfRowsInsection method and reload table view at appropriate place
in your cellForRowAtIndexPath: add these lines
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
Check this tutorial, I'm sure it will help you
UItableView Guide
and for scrolling, You don't need to do anything yourselves, as the number of cells increases beyond the provided size, it automatically becomes scrollable and make sure that scrolling is enabled in your UITableView attribute inspector, check the image
Happy Coding
Regards
Use below code.....define alphabetarr in globally then use it in code...
- (void)viewDidLoad
{
alphabetarr = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k",#"l",#"m",#"n",#"o",#"p",#"q",#"r",#"s",#"t",#"u",#"v",#"w",#"x",#"y",#"z", nil];
CGRect frame = self.view.frame;
UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
tableview.backgroundColor = [UIColor cyanColor];
tableview.separatorColor = [UIColor blackColor];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];
return cell;
}

Resources