TableView do not enter in willDisplayCell - iphone

My goal is define my own cell style, with background, font and size. I try this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ScreenListElements *currentScreenElement = [self.screenDefBuild.elementsToTableView objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentScreenElement.objectName];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:currentScreenElement.objectName];
}
cell.textLabel.text = currentScreenElement.objectName;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.backgroundColor = [UIColor blackColor];
}
I would like to change cell backgrond, but my app doesn't enter to willDisplayCell method. I have declared my class as:
#interface MyTableView : UIViewController <UITableViewDataSource,NSCopying> {
}
Should i have something else? Or maybe is a better way to declare own cell style.

#interface MyTableView : UIViewController <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}
the method
tableView:willDisplayCell:forRowAtIndexPath
is delegate method of UITableView
EDITED (to make it correct & accepted answer)
setting delegate
[myTableView setDelegate:self];

Ok, i saw, that you didn't implement the "UITableViewDelegate" protocol, only dataSource...
So it surely doesn't enter delegate methods.
Be sure it is like so:
#interface MyTableView : UIViewController <UITableViewDataSource, UITableViewDelegate ,NSCopying> {
}

Check UITableviewCell Delegate Protocol :
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor blackColor];
}

I had the same issue where I set all the things correct but if you have your function written manually you might miss the syntax and this error occurs:
(Instance method 'tableView(tableView:willDisplayCell:forRowAtIndexPath:) nearly
matches optional requirement tableView(_:willDisplay:forRowAt:) of
protocol UITableViewDelegate
This way the function is not going to be recognised, write the function correctly:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
If the delegate is assigned to self this will work, else it's gonna show the error as you write the code.

Related

Grouped Style UITableView doesn't clip subviews

I have a design issue with grouped uitableview, I add uiviews to the leftmost side of each cell extending little over the cell bounds and I want them to be clipped. But neither setting clip subviews in IB nor setting clipsToBounds property in code didn't help.
So far I have set clipsToBounds (for both uitableview itself and individual uitabviewcells) in a number of places but none helped:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setClipsToBounds:TRUE];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
if (cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:defaultIdentifier withIndexPath:indexPath];
}
...
[cell setClipsToBounds:TRUE];
...
return cell;
}
- (void)viewDidLoad
{
...
[mainTableView setClipsToBounds:TRUE];
}
Is it possible without changing tableview style to plain?
Update: My previous answer was wrong. Here is an answer that actually shows how to do what you're asking.
https://stackoverflow.com/a/401271/350467
.......
Previous Answer
If you're adding views to the cell's contentView (which you should be, unless you have a good reason), you'll want to set cell.contentView.clipsToBounds = YES;

Renaming the "Delete" button in edit mode of tableviewcontroller

Can we rename the "Delete" button when put a table view in edit mode to something else?
The UITableView delegate just has to implement:
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
Implement
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
And return your custom title for delete.
From UITableViewDelegate docs
In Swift:
override func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "Remove"
}
Code:
*- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"Rename"; // or put something else
}*

How to reach the current selected cell in didSelectRowAtIndexPath?

I want to change the accessory view type of a cell through the method: didSelectRowAtIndexPath , i.e. when a row is selected I want to change the accessory view type, can I do this inside that method ?
You can get the cell from didSelectRowAtIndexPath: method using the indexPath like this.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
In Swift 3.0 : You can use these 2 ways, according to your need
let cell:UITableViewCell = tableView.cellForRow(at: indexPath) as UITableViewCell
or
let cell:CustomTableViewCell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
Happy Coding !!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// Access accessory View as below.
UIView * myCellAccessoryView = myCell.accessoryView;
}
Use the below function and pass the index path of the selected row so that the particular cell is reloaded again.
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Another solution: store the selected row index and do a reload of tableview. Then in cellForRowAtIndexPath check for the selected row and change the accessory view of the cell.
Swift
You can get the cell from didSelectRowAtIndexPath: method using the indexPath like this:
let cell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
Store selected indexpath. for eg:
NSInteger selectedIndexPath;
then follow the step below. set your accessory view.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AddressTableViewCell *cell=(AddressTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.backgroundColor=[UIColor lightGrayColor];
Address *addr=[self.locationArray objectAtIndex:indexPath.row];
cell.titleLabel.text = addr.addressTitle;
cell.detailLabel.text = addr.addressDetail;
if (indexPath.row == selectedIndexPath) {
[cell.selectButton setImage:[UIImage imageNamed:#"checkboxFilled"] forState:UIControlStateNormal];
}
else {
[cell.selectButton setImage:[UIImage imageNamed:#"checkbox"] forState:UIControlStateNormal];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedIndexPath=indexPath.row;
[self.tableView reloadData];
}

Get textLabel text of selected Cell using the iPhone SDK

I would like to know how to get the textLabel string value of the selected uitableviewcell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// now you can use cell.textLabel.text
}
You can get the cell using [self.tableView cellForRowAtIndexPath:] and then access its textLabel.text property, but there's usually a better way.
Typically you've populated your table based on some model array that your UITableViewController has access to. So a better way to handle this in most cases is to take the row number of the cell that was selected and use that to find the associated data in your model.
For example, let's say your controller has an array of Buddy objects, which have a name property:
NSArray *buddies;
You populate this array by running a query or something. Then in tableView:cellForRowAtIndexPath: you build a table view cell based on each buddy's name:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BuddyCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"BuddyCell"] autorelease];
}
cell.textLabel.text = [buddies objectAtIndex:indexPath.row];
return cell;
}
Now when the user selects a row, you just pull the corresponding Buddy object out of your array and do something with it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Buddy *myBuddy = [buddies objectAtIndex:indexPath.row];
NSLog (#"Buddy selected: %#", myBuddy.name);
}
if (selected)
{
indicator.image = [UIImage imageNamed:#"IsSelected.png"];
[arrSlectedItem addObject:strselecteditem];
NSLog(#"-- added name is %#", strselecteditem);
}
else
{
indicator.image = [UIImage imageNamed:#"NotSelected.png"];
[arrSlectedItem removeObject:strselecteditem];
NSLog(#"--- remove element is -- %#", strselecteditem);
}
If anyone stumbles across this and is wondering how to do this in swift the code is below. Also remember to use optional binding to unwrap that optional and also avoid printing out "Optional("Tapped Item Label")".
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
// Unwrap that optional
if let label = cell?.textLabel?.text {
println("Tapped \(label)")
}
}
Here what i used;very simple
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete
{
//create cellobj with indexpath for get it text
let cell = tableView.cellForRowAtIndexPath(indexPath)
let celltext = (cell?.textLabel?.text!)! as String
//do what ever you want with value
print((cell?.textLabel?.text!)! as String)
}
}

reordering control in UITableView

I am developing a game in which I am using a UITableView which has custom cell (UItableViewCell subclass).
In editing mode:
Only the reordering control of the UITableView should show.
Right now I am getting the delete and the reordering control.
How to get only reordering control while editing ?
I know this answer might be late, but I'm putting it just for people who still need it.
Just implement the following methods:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
You should use this method for hiding delete button.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
thanks a lot oxigen it worked ....
i was writing this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.showsReorderControl = YES; // to show reordering control
return cell;
}
but i dint write the method which you have given
Thanks a tons
Swift 5.0
If you want to disable all other editing options for your tableView's cells except the one that allows you to reorder them then just implement those methods from UITableViewDelegate:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
true
}
// Removes the ability to delete current cell
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
.none
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
true
}
tableView.showsReorderControl = YES; // to show reordering control
To dissmiss delete control, in your UITableViewDelegate add
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}