I need to change the text's cell when the device rotate. This is my code:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.lista beginUpdates];
NSIndexPath *durPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *paths = [NSArray arrayWithObjects:durPath, nil];
UITableViewCell *celda = [self.lista cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:0 inSection:0]];
celda.textLabel.text = #"new text for the cell";
[self.lista reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];
[self.lista endUpdates];
}
and nothing happens. What's wrong?
Related
Ok so I have 7 NSMutableArrays and they are connected to a tableview
the first table-view tells the second table-view which one to load,
and it loads fine, but each one I want to be able to add a item/cell, but
I could only get it to load for one of my views.
Here is my NSMutableArrays list of items and properties.
Properties:
#property (nonatomic) NSMutableArray *Movies;
#property (nonatomic) NSMutableArray *Tv_Shows;
#property (nonatomic) NSMutableArray *Resturuants;
#property (nonatomic) NSMutableArray *Video_Games;
#property (nonatomic) NSMutableArray *Concert_Venues;
#property (nonatomic) NSMutableArray *Foods;
#property (nonatomic) NSMutableArray *Recipes
NSMutableArray items
_Movies = [[NSMutableArray alloc]initWithObjects:#"Avatar", #"Kung Fu Panda", #"The Matrix", nil];
_Tv_Shows = [[NSMutableArray alloc]initWithObjects:#"Reguluar Show", #"Kung Fu Panda", #"Avatar The last Airbender", nil];
_Resturuants = [[NSMutableArray alloc]initWithObjects:#"Outback Steakhouse", #"Golden Corral", #"Wendy's", nil];
_Video_Games = [[NSMutableArray alloc]initWithObjects:#"black Ops", #"Call Of Duty", #"Modern Warfare", nil];
_Foods = [[NSMutableArray alloc]initWithObjects:#"Baked Bread", #"Pasta", #"Italian Meatballs", nil];
_Recipes = [[NSMutableArray alloc]initWithObjects:#"Chocolate Cookies", #"Brownies", #"Peanutbutter Fudge", nil];
_Concert_Venues = [[NSMutableArray alloc]initWithObjects:#"Pheonix", #"Buckeye", #"Maricopa", nil];
Unwind To List Method it only loads for the first item.
- (IBAction)unwindToTableViewController:(UIStoryboardSegue *)sender {
AddViewController *addViewController = (AddViewController *)sender.sourceViewController;
NSString *text = addViewController.textField.text;
//if NOT blank and NOT whitespace
if (![text length] == 0 && ![[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
//Add it to the top of the data source.
[_Movies insertObject:text atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//Insert it into the tableview
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}
//Add it to the top of the data source.
[_Movies insertObject:text atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
As you can see only _movies is being loaded, you have to do the same for _TvShows,Restaurants, etc... with increasing values (as in not 0). For example
[_Restaruants insertObject:text atIndex:1];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
//you may have to keep inSection:0, try it with 1 anyway
I figured it out!
- (IBAction)unwindToTableViewController:(UIStoryboardSegue *)sender {
AddViewController *addViewController = (AddViewController *)sender.sourceViewController;
NSString *text = addViewController.textField.text;
//if NOT blank and NOT whitespace
if (![text length] == 0 && ![[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
//Add it to the top of the data source.
[_Movies insertObject:text atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//Insert it into the tableview
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}
Add Items under the _Movies
like so
[_Movies insertObject:text atIndex:0];
then add new ones
currently my situation is my fist row is checked by default. but when i want to uncheck it, it need user to click twice to uncheck it. may i know where i done wrongly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Report_SelectGroupTableViewCell";
Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Report_SelectGroupTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.indexPath = indexPath;
cell.delegate = self;
}
// [myTableView selectRowAtIndexPath:0 animated:NO scrollPosition:UITableViewScrollPositionNone];
//[self tableView:myTableView didSelectRowAtIndexPath:0];
[cell setSelectionStyle:NO];
NSString *groupID = [[groupArray objectAtIndex:indexPath.row] objectForKey:#"group_id"];
if ([groupID isEqualToString:selectedGroup]){
NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];
BOOL isSelected = [[dict objectForKey:#"isSelected"] boolValue];
if (!isSelected) {
isSelected = !isSelected;
[dict setObject:[NSNumber numberWithBool:isSelected] forKey:#"isSelected"];
}
cell.userInteractionEnabled = NO;
}
cell.groupName.text = [[groupArray objectAtIndex:indexPath.row] objectForKey:#"group_name"];
NSMutableDictionary *typeDict = [groupArray objectAtIndex:indexPath.row];
cell.tickButton.selected = [[typeDict objectForKey:#"isSelected"] boolValue];
if (cell.tickButton.selected) {
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon_empty.png"] forState:UIControlStateNormal];
}
}
else{
[cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
}
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon.png"] forState:UIControlStateNormal];
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
[typeDict setObject:#"1" forKey:#"isSelected"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];
BOOL isSelected = [[dict objectForKey:#"isSelected"] boolValue];
isSelected = !isSelected;
[dict setObject:[NSNumber numberWithBool:isSelected] forKey:#"isSelected"];
if (cell.tickButton.isSelected == YES)
{
[cell.tickButton setSelected:NO];
[cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon_empty.png"] forState:UIControlStateNormal];
}
}
else if (cell.tickButton.isSelected == NO)
{
[cell.tickButton setSelected:YES];
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
}
}
You should remove
[myTableView selectRowAtIndexPath:0 animated:YES scrollPosition:
UITableViewScrollPositionNone];
and use this to select the first row (this should be called once outside this method or you want to set this row selected everytime each cell is render)
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
And I don't know why you called this in this method
[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:0];
but if you want to call it you should change to this too
[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
ps. You should set the cell's selectionStyle correctly to see the cell is selected.
Update::
Try to replace all codes in the method with this
Report_SelectGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Report_SelectGroupTableViewCell"];
if (!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:#"Report_SelectGroupTableViewCell" owner:self options:nil] objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
return cell;
Please tell me if the first row is not selected and highlighted.
I want to this works out of cellForRowAtIndexPath:
cell.textLabel.textColor = [UIColor blueColor];
Retrieve the cell by calling cellForRowAtIndexPath: with an NSIndexPath object with its row set to zero and then set the colour. In your IBAction method, do this:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor blueColor];
I did a UITableView filled with a plist data source, and I did custom cells with Interface Builder (so I'm using xib files)
here's the part of code where I'm creating cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"DataTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[UITableViewCell class]]) {
cell = (DataTableViewCell*)view;
}
}
}
return cell;
}
then when I use the:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
method to get the selected cell, I call this method to expand the cell:
- (void)expandCellAtIndex:(int)index {
NSMutableArray *path = [NSMutableArray new];
NSArray *currentCells = [subCellItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++) {
[path addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Now the problem is that since I've not done this before, I'm stuck on the logic on how to change the cell I want to show when expanded, that's because the method:
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
is searching for the right path and inserting a cell with the same cell xib file as the one I've loaded with my data at the start
how can I set a different cell (with a different xib, and different data) to show instead of the same as before?
basically I have this table working but in the expanding cell I see a copy of the cell you touch
You can do it like this:
In .h:
#interface TSViewController : UIViewController
{
NSMutableArray* subCellIndexPaths;
}
In DataTableViewSubCell.xib create one "Table View Cell" (other Views you have to remove). Change Class for "Custom Class" to DataTableViewSubCell. Replay it for second cell.
In .m:
- (UITableViewCell*) tableView: (UITableView*) tableView
cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
static NSString *CellIdentifier = #"Cell";
DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
if ([subCellIndexPaths containsObject: indexPath])
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSuperCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
else
{
NSArray* nib = [[NSBundle mainBundle] loadNibNamed: #"DataTableViewSubCell"
owner: nil
options: nil];
cell = [nib objectAtIndex: 0];
}
}
return cell;
}
- (void) expandCellAtIndex: (int)index
{
NSMutableArray* indexPaths = [NSMutableArray new];
NSArray* currentCells = [subCellItems objectAtIndex: index];
int insertPos = index + 1;
for (int i = 0; i < [currentCells count]; i++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow: insertPos++
inSection: 0];
[indexPaths addObject: indexPath];
[subCellIndexPaths addObject: indexPath];
}
[self.tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: index
inSection: 0]
atScrollPosition: UITableViewScrollPositionTop
animated: YES];
}
And of course you must create subCellIndexPaths anywhere (in init or viewDodLoad methods).
I know this might sound like a dumb question, but I have looked every where. How can I do this?
I know how to do this with a swype-to-delete method, but how cam I do it outside that function?
Please post some code samples.
Thanks!Coulton
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
insertIndexPaths is an array of NSIndexPaths to be inserted to your table.
deleteIndexPaths is a array of NSIndexPaths to be deleted from your table.
Example array format for index paths :
NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];
In Swift 2.1+
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
tableView.endUpdates()
And you can create an NSIndexPath easily like so:
NSIndexPath(forRow: 0, inSection: 0)
You want these two methods: insertRowsAtIndexPaths:withRowAnimation: and deleteSections:withRowAnimation: They are both detailed in the UITableView documentation.
Swift 4.0
tableView.beginUpdates()
tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
tableView.endUpdates()
For creating IndexPath use this:
IndexPath(row: 0, section: 0)