Adding target/action for custom UITableViewCell - iphone

So I can't figure out what I'm doing wrong. I have two cells, each in their own section. Both have a UISegmentedControl in them with their own outlet/xib, etc. Both rows show up fine in the simulator, but only the first cell (SortByTableViewCell) will have the action called when the UISegmentedControl is pressed. In the second cell, the UISegmentedControl does not crash the app, but it also does not call its selector. Is there something obvious that I am missing? Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
if (section == 0) {
static NSString *SortByCellIdentifier = #"SortByCellIdentifier";
SortByTableViewCell *cell = (SortByTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SortByCellIdentifier];
[cell.SortBySegmentedControl addTarget:self action:#selector(SortBySegmentedControlPressed:) forControlEvents:UIControlEventValueChanged];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"SortByTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[SortByTableViewCell class]]) {
cell = (SortByTableViewCell *)currentObject;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else {
static NSString *ConditionCellIdentifier = #"ConditionCellIdentifier";
ConditionTableViewCell *cell = (ConditionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ConditionCellIdentifier];
[cell.ConditionSegmentedControl addTarget:self action:#selector(ConditionSegmentedControlPressed:) forControlEvents:
UIControlEventValueChanged];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"ConditionTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[ConditionTableViewCell class]]) {
cell = (ConditionTableViewCell *)currentObject;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Why setting the addTarget before your cell initialization?
Try with:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
if (section == 0) {
static NSString *SortByCellIdentifier = #"SortByCellIdentifier";
SortByTableViewCell *cell = (SortByTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SortByCellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"SortByTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[SortByTableViewCell class]]) {
cell = (SortByTableViewCell *)currentObject;
}
}
}
[cell.SortBySegmentedControl addTarget:self action:#selector(SortBySegmentedControlPressed:) forControlEvents:UIControlEventValueChanged];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else {
static NSString *ConditionCellIdentifier = #"ConditionCellIdentifier";
ConditionTableViewCell *cell = (ConditionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ConditionCellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"ConditionTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[ConditionTableViewCell class]]) {
cell = (ConditionTableViewCell *)currentObject;
}
}
}
[cell.ConditionSegmentedControl addTarget:self action:#selector(ConditionSegmentedControlPressed:) forControlEvents:
UIControlEventValueChanged];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Hope it's solve your problem.

Have you tried removing the connection in Interface Builder and recreating it?

Related

return type for CellForRowAtIndexPath

i have two tableviews in my view. i have issue in implementing the delegate methods. Can anyone help me on this. Here the problem is, this method requires a return value of type UITableViewCell right. what should i return?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
return cell;
}
if (tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"ContactListCustomCell";
ContactListCustomCell *cell = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
return cell;
}
}
Use this edited code. It will also get rid you of any warning regarding return cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifier_contact = #"ContactListCustomCell";
UITableViewCell *cell;
if(tableView==ContactTableview)
{
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *totalString = [ContactArray objectAtIndex:indexPath.row];
NSArray *afterSeparate = [[NSArray alloc]init];
afterSeparate = [totalString componentsSeparatedByString:#"+"];
NSString *cellText = [afterSeparate objectAtIndex:0];
//NSString *detailText = [afterSeprate objectAtIndex:1];
cell.textLabel.text = cellText;
//cell.detailTextLabel.text=detailText;
}
if (tableView==ContactTableViewLabel)
{
ContactListCustomCell *cell1 = (ContactListCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContactListCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell1 = (ContactListCustomCell *) currentObject;
break;
}
}
}
cell1.ContactLabel.text = [LabelArray objectAtIndex:indexPath.row];
cell1.ContactValue.text = [ValueArray objectAtIndex:indexPath.row];
cell = (UITableViewCell*) cell1;
}
return cell;
}
There is no need to cast ContactListCustomCell as a UITableViewCell. You can use ContactListCustomCell as a subclass of UITableViewCell. You can update your code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(tableView==ContactTableview)
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
.
.
}
else if(tableView==ContactTableViewLabel)
{
static NSString *CellIdentifier = #"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"yourCellNibName" owner:self options:nil];
cell = self.yourCellProperty;
self.yourCellProperty = nil;
}
.
.
}
return cell;
}
GoodLuck !!!

Can not able to Select Cell from tableView

Hi friends it is a Strange Issue I'm facing, I am using Multiple UItableCustomCells in one Grouped TableView with Section's.
I am able to Select first Section's rows only, then when I click on another Section's row it's selection method is not working, I can not understand where is mistake, my cellForRowAtIndexPath code is Below:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
if(indexPath.section==0)
{
if(indexPath.row==0)
{
[[NSBundle mainBundle]loadNibNamed:#"CustomCell1" owner:self options:nil];
cell = self.Cell1;
self.Cell1 = nil;
txtincidentName = (UITextField*)[cell.contentView viewWithTag:1];
}
else if(indexPath.row==1)
{
[[NSBundle mainBundle]loadNibNamed:#"CustomCell2" owner:self options:nil];
cell = self.Cell2;
self.Cell2 = nil;
lblDatefirst = (UILabel*)[cell.contentView viewWithTag:2];
btnCalfirst = (UIButton*)[cell.contentView viewWithTag:3];
lblTimeFirst = (UILabel*)[cell.contentView viewWithTag:4];
}
else if(indexPath.row==2)
{
[[NSBundle mainBundle]loadNibNamed:#"CustomCell2" owner:self options:nil];
cell = self.Cell2;
self.Cell2 = nil;
lblDatefirst = (UILabel*)[cell.contentView viewWithTag:2];
btnCalfirst = (UIButton*)[cell.contentView viewWithTag:3];
lblTimeFirst = (UILabel*)[cell.contentView viewWithTag:4];
lblInciTime =(UILabel*)[cell.contentView viewWithTag:9];
lblInciTime.text=#"Date/Time";
}
}
else if(indexPath.section==1)
{
if(indexPath.row==0)
{
[[NSBundle mainBundle]loadNibNamed:#"CustomCell3" owner:self options:nil];
cell = self.Cell3;
self.Cell3 = nil;
btndropDown1 = (UIButton*)[cell.contentView viewWithTag:5];
btndropDown2 = (UIButton*)[cell.contentView viewWithTag:6];
btndropDown3 = (UIButton*)[cell.contentView viewWithTag:7];
}
else if(indexPath.row==1)
{
[[NSBundle mainBundle]loadNibNamed:#"CustomCell4" owner:self options:nil];
cell = self.Cell4;
self.Cell4 = nil;
txtViewofdetails = (UITextView*)[cell.contentView viewWithTag:7];
}
}
return cell;
}
simulator image
Please Help and guide Thank you..
Don't load the cells from nibs like this. Instead register the cells with a reuse identifier in the NIB. This will cause the dequeueing pick the correct cell protoype based on the identifier.
Most likely your current approach messes up the touch handling of the table view.
hey try this type of concept and flow of the code
NSString *CellIdentifier =[NSString stringWithFormat:#"%d%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell1" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell1 *) currentObject;
break;
}
}
}

Property 'length' not found on object of type 'id'

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.nameLabel.text = [arrareasdata objectAtIndex:indexPath.row];
cell.designLabel.text = [arrareasdata1 objectAtIndex:indexPath.row];
if(([arrareasdata2 objectAtIndex:indexPath.row].length > 0)
{
cell.emailLabel.text = [arrareasdata2 objectAtIndex:indexPath.row]
}
else
{
cell.emailLabel.text = #"";
}
cell.webLabel.text = [arrareasdata3 objectAtIndex:indexPath.row];
return cell;
}
the problem is here
if(([arrareasdata2 objectAtIndex:indexPath.row].length > 0)
what you must do is get object from the array and cast it into appropriate type. for example if the objects in array are NSString type then do following
NSString *value = (NSString*)[arrareasdata2 objectAtIndex:indexPath.row];
then in your if statement check for length
if (value.length > 0)

Custom TableViewCells on a UIViewController?

I am trying to display a custom TableView on a UIViewController but am getting an error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:"
I had connected the TableView to datasource and delegate.
Any suggestion to go about implementing so or do I need a UITableViewController?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:nil options:nil];
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[UITableView class]]){
cell = (CustomCell *) currentObjects;
break;
}
}
}
//---set the text to display for the cell---
cell.cellNameLabel.text = #"This is name";
cell.cellValueLabel.text = #"This is Value";
return cell;
ERROR:
//NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:nil options:nil];
//in above owner should be self
// if ([currentObjects isKindOfClass:[UITableView class]]){
change this line to
if ([currentObjects isKindOfClass:[CustomCell class]]){
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomCell"
owner:self options:nil];//owner should be self
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[CustomCell class]]){
cell = (CustomCell *) currentObjects;
break;
}
}
}
//---set the text to display for the cell---
cell.cellNameLabel.text = #"This is name";
cell.cellValueLabel.text = #"This is Value";
return cell;
}

request for member error when loading in customtableviewcell and setting its IBOUTLETS

I've spent the last 3 hours scratching my head at this issue because all seems correct, except I can't get through to my tableviewcell class to set the uilabels in my custom cell.
here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
//cell.imageViewSection =
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *textTitle = [[stories objectAtIndex: storyIndex] objectForKey: #"title"];
NSURL *imageurl = [NSURL URLWithString:[[stories objectAtIndex: storyIndex] objectForKey: #"description"]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:imageurl]];
cell.imageViewSection = image;
cell.titleLabelText.text = textTitle;
return cell;
}
If someone can help me out it would be awesome :)
You should not rely on the cell always being returned as object at index 0 in the array. Do a simple loop to find the actual cell.
And add typecasts for the cell subclass you use
-(UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* cellID = #"cellID";
MyCell* cell = (id)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell"
owner:self
options:nil];
for (cell in nib) {
if ([cell isKindOfClass:[MyCell class]]) {
break;
}
}
}
// Safely do tuff to cell
return cell;
}
This snippet assumes that the cell is at least available, the behavior if no table view cell is returned is undefined.
You're assigning and casting TableViewCell to UITableViewCell variable and UITableViewCell does not have those outlets. Instead, you should do the following..
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = (TableViewCell *)[nib objectAtIndex:0];
}