First of all I know there are several more titles looks like same. I have checked them but what I ask is a specific problem.
What I want to do: Created Login table which includes email and password. UITextfields in UITableView. Just want to take those data into some NSString variable pointers for further process.
I have a Custom Cell Class named CustomCell, as below:
#import "CustomCell.h"
#import "QuartzCore/QuartzCore.h"
#implementation CustomCell
#synthesize textfield;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
textfield = [[UITextField alloc] initWithFrame:CGRectMake(5, 7, 160, 20)];
textfield.textColor = [UIColor blackColor];
textfield.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:textfield];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:NO animated:NO];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
And I have ViewController where I use the custom table below you may see my tableView cellForRowAtIndex method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
[tableView.layer setCornerRadius:10.0];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}
switch (indexPath.row) {
case 0:
cell.textfield.placeholder = #"Email";
break;
case 1:
cell.textfield.placeholder = #"Password";
cell.textfield.tag = 0;
break;
}
return cell;
}
And lastly below in same class I am trying to read email & password with
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // ERROR!!!
NSString *userpassword = cell.textfield.text;
Error: Unknown receiver type tableView:
Attention in same line:
ClassMethod +cellforrowatindexpath not found return type defaults to id.
Did you see what I am doing wrong?
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; // Add This instead
NSString *userpassword = cell.textfield.text;
Yu can't use tableView outside the datasource and delegate methods. What's the name of your tableview in your interface definition? Use that one.
Is your ViewController inheriting UIView or UITableViewController? This is assuming you are calling tableView on the same controller as the Table View.
Related
I have a textfield in the MyCustomCell, and set the textfield delegate in the cellForRowIndexPath tableview delegate. When user changes the textfield value and it calls textFieldShouldEndEditing. All it works.
However, I wonder how to know which textfield (indexPath) is changed?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
// use this if you created your cell with IB
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner:self options:nil] objectAtIndex:0];
// otherwise use this
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// now set the view controller as the text field delegate
cell.textField.delegate = self;
}
// configure cell...
return cell;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Set UITextField tag in your cellForRowAtIndexPath method. If there is only one section in your tableview you can set the row as tag. Like this.
cell.textField.delegate = self;
cell.textField.tag = indexPath.row;
And in your textFieldShouldEndEditing method you can check the tag.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
int row = textField.tag;
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if ([[[textField superview] superview] isKindOfClass:[MyCustomCell class]]) {
MyCustomCell *cell = (MyCustomCell *)[[textField superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// this is what you want.
}
return YES;
}
You Can Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
// use this if you created your cell with IB
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner:self options:nil] objectAtIndex:0];
// otherwise use this
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// now set the view controller as the text field delegate
cell.textField.delegate = self;
textField.tag = indexPath.Row;
}
// configure cell...
return cell;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog("Selected Cell is:%#",textField.tag);
[textField resignFirstResponder];
return YES;
}
I'm trying to get the indexPath.row of a button clicked inside a tableView row.
When the user clicks this button I get the index.row corresponding to the button very well, but when I add more objects to the source array to create more cells by calling reloadData, the rowButtonClicked in each cell it's no longer giving me the correct indexPath.row in example I press the index 20 and now the printed indexPath.row is 9.
In cellForRowAtIndexPath to add the event to the button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
lBtnWithAction = [[UIButton alloc] initWithFrame:CGRectMake(liLight1Xcord + 23, 10, liLight1Width + 5, liLight1Height + 25)];
lBtnWithAction.tag = ROW_BUTTON_ACTION;
lBtnWithAction.titleLabel.font = luiFontCheckmark;
lBtnWithAction.tintColor = [UIColor blackColor];
lBtnWithAction.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:lBtnWithAction];
}
else
{
lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];
}
//Set the tag
lBtnWithAction.tag = indexPath.row;
//Add the click event to the button inside a row
[lBtnWithAction addTarget:self action:#selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
To do something with the clicked index:
-(void)rowButtonClicked:(UIButton*)sender
{
//Get the index of the clicked button
NSLog(#"%li", (long)sender.tag);
[self doSomething:(long)sender.tag];
}
Constants.h
#define ROW_BUTTON_ACTION 9
Why it is giving an incorrect index when I change the initial items of the tableView? Is there a way to solve this issue?
It looks like you're messing up button tags. Once you set the tag
lBtnWithAction.tag = indexPath.row;
you won't be able to get button correctly with
lBtnWithAction = (UIButton *)[cell.contentView viewWithTag:ROW_BUTTON_ACTION];
(assuming ROW_BUTTON_ACTION is a constant). lBtnWithAction will be nil all the time except when indexPath.row is equal to ROW_BUTTON_ACTION.
I would propose to subclass UITableViewCell, add a button-property there and then just refer to it directly instead of searching by tag. In this case you'll be able to use tags for buttons freely :) –
#interface UIMyTableViewCell : UITableViewCell
#property (nonatomic, strong, nonnull) UIButton *lBtnWithAction;
#end
And then in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UIMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.lBtnWithAction addTarget:self action:#selector(rowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
cell.lBtnWithAction.tag = indexPath.row;
return cell;
}
You can simply update you line -
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
My take on this will be subclassing the UITableViewCell and providing a delegate for it to respond.
code:
//MyCoolTablewViewCell.h
#protocol MyCoolProtocol<NSObject>
-(void)youTouchedMyCoolCell:(MyCoolTableViewCell __nonnull *)myCoolCell;
#end
#interface MyCoolTableViewCell:UITableViewCell
#property(nonatomic, weak,nullable) id<MyCoolProtocol>myCooldelegate;
#end
//MyCoolTablewViewCell.m
#interface MyCoolTableViewCell(){
UIButton *mySuperCoolButton;
}
#end
#implementation MyCoolTablewViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if(!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
return nil;
}
// init your stuff here
// init button
mySuperCoolButton = [UIBUtton buttonWithType:UIButtonTypeCustom];
[mySuperCoolButton addTarget:self action:#selector(tappedMyCoolButton:) forControlEvents:UIControlEventTouchUpInside];
return self;
}
- (void)tappedMyCoolButton:(id)sender{
if([_myCoolDelegate respondToSelector:#selector(youTouchedMyCoolCell:)]){
[_myCoolDelegate youTouchedMyCoolCell:self];
}
}
#end
then in your controller
// whatEverController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get the cell
cell = [tableView dequeueReusableCellWithIdentifier:#"myCoolIdentifier"forIndexPath:indexPath];
cell.myCooldelegate = self;
}
-(void)youTouchedMyCoolCell:(MyCoolTableViewCell *)myCoolCell{
NSIndexPath *myCoolIndexPath = [_tableView indexPathForCell:myCoolCell];
// then do whatever you want with the index path
}
I have a UITableView with two different custom table cells. The first cell appears normal after I start the app. The second cell will appear when you click on them.
Can anybody help me or has an idea?
Thanks a lot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"customCell2";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont fontWithName:#"STHeitiSC-Light" size:9.0];
}
return cell;
}
Having done custom UITableViewCell in the past I usually handle the nib loading in the custom class itself.
The basic header for the custom cell.
#interface RequestsTableViewCell : UITableViewCell {
// Ivars.
}
// Properties.
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType;
// Other methods, etc.
#end
The custom cell with a designated initializer.
#implementation RequestsTableViewCell
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"RequestsTableViewCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
requestModel = model;
queryType = requestType;
[self setRequestThumbnail];
[self setRequestCategory];
[self setRequestAddress];
[self setRequestStatusDate];
[self setRequestStatus];
[self setRequestFollowed];
[self setRequestComment];
[self setAppearance];
}
return self;
}
There would also be a custom xib for the custom UITableViewCell that corresponds and has the custom class set in the identity inspector.
In the UITableViewController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell Id";
RequestModel *request = nil;
// Other code for search, etc
request = [self.serviceRequests objectAtIndex:indexPath.row];
RequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
cell = [[RequestsTableViewCell alloc] initWithRequestModel:request style:UITableViewCellStyleDefault reuseIdentifier:cellId forQueryType:queryTypeIndicator];
}
return cell;
}
It also sounds like you have more than one custom cell type in your question? Can you elaborate on how it is all supposed to function? You say that you have to click one cell to make another appear, can you explain that interaction?
I did something similar, but made the cell 'expand', instead of adding a new cell. Of course then you don't have two cells, but you can resize your one cell, add subframes,...
You can keep a boolean in your UITableViewCell object (BOOL cellIsExpanded), and set that on tap gesture. Then in drawRect of the TableViewCell, layout your cell accordingly.
Example code, on expand, make cell height 20-->80 and add a UIButton:
In the TableViewController, overload heightForRowAtIndexPath (this will resize your cell if 'expanded'):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourEntity *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (!record.cellIsExpanded)
return 20.; // cell is smaller if collapsed
else
return 80.; // bigger cell
}
In the TableViewCell, add or remove subframes:
#interface MyTableViewCell ()
#property(nonatomic) BOOL cellIsExpanded
#property(strong, nonatomic) UITextField *myTextField;
#property(strong, nonatomic) UIButton *clickMeButton;
#end
#implementation MyTableViewCell
- (void)drawRect:(CGRect)rect {
if(!self.cellIsExpanded){
// layout your collapsed cell, for example:
self.myTextField = [[UITextField alloc] initWithFrame:self.frame];
self.myTextField.text = #"Collapsed cell";
// remove button, only present in expanded view :
self.clickMeButton=nil;
}
else{
self.myTextField.text = #"Expanded cell";
// add button below textfield
self.clickMeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 10, 10)];
}
}
#end
Here's my code:
- (UITableView *)table {
if (!_table) {
_table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
[_table setDelegate:self];
[_table setDataSource:self];
[_table registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
return _table;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
else
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
The problem is when I registerClass for my table, it assumes that my cell style is UITableViewCellStyleDefault. So that's why detailTextLabel doesn't show up. I tested it.
Comment out the registerClass line doesn't work because I don't have any CellIdentifier for dequeueReusableCell. So it will throw some exceptions.
If I'm not using dequeue, it works, but it not the best practice.
AFAIK, table cell couldn't change its style after init. So how can I make the detailTextLabel show up ?
The problem is when I registerClass for my table, it assumes that my cell style is UITableViewCellStyleDefault. So that's why detailTextLabel doesn't show up
That is correct. The solution is: do not register UITableViewCell as your class. Register a custom UITableViewCell subclass whose sole purpose in life is that it initializes itself to a different style.
For example, register the MyCell class, which you have defined like this:
#interface MyCell:UITableViewCell
#end
#implementation MyCell
-(id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:UITableViewCellStyleValue2 // or whatever style you want
reuseIdentifier:reuseIdentifier];
return self;
}
#end
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.