Iterate Over All the Uitablecells Given a Section Id

Iterate over all the UITableCells given a section id

To answer my own question: "how can I iterate over all the UITableCells given a section id?":

To iterate over all the UITableCells of a section section one must use two methods:

  • tableView.numberOfRowsInSection(section)
  • tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))

So the iteration goes like this:

// Iterate over all the rows of a section
for (var row = 0; row < tableView.numberOfRowsInSection(section); row++) {
var cell:Cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))?

// do something with the cell here.
}

At the end of my question, I also wrote a note: "Note: I want to set the AccesoryType to None for all of the cells in a section, programatically". Notice that this is a note, not the question.

I ended up doing that like this:

// Uncheck everything in section 'section'
for (var row = 0; row < tableView.numberOfRowsInSection(section); row++) {
tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section))?.accessoryType = UITableViewCellAccessoryType.None
}

If there is a more elegant solution, go ahead and post it.

Note: My table uses static data.

iPad: Iterate over every cell in a UITableView?

for (int section = 0; section < [tableView numberOfSections]; section++) {
for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:cellPath];
//do stuff with 'cell'
}
}

How can I loop through UITableView's cells?

If you only want to iterate through the visible cells, then use

NSArray *cells = [tableView visibleCells];

If you want all cells of the table view, then use this:

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}

Now you can iterate through all cells:

(CustomTableViewCell is a class, which contains the property textField of the type UITextField)

for (CustomTableViewCell *cell in cells)
{
UITextField *textField = [cell textField];
NSLog(@"%@"; [textField text]);
}

How to iterate a for loop in table view cell swift

The basics of what you are trying to do are to get a value from the data source, convert it to a number if needed and then total them up.

For your example above, this would do the trick:

let news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

let total = news.map({ Int($0)! }).reduce(0) { x, y in
return x + y
}

print(total) // 55

To do this for a shopping cart, your data source will likely be a list of products instead, maybe something like:

struct Product
{
var name: String
var price: Int
var quantity: Int
}

let prod1 = Product(name: "Coka Cola", price: 2, quantity: 2)
let prod2 = Product(name: "Bread", price: 1, quantity: 1)
let prod3 = Product(name: "Sweets", price: 2, quantity: 4)

let shoppingCart = [prod1, prod2, prod3]

let total = shoppingCart.reduce(0) { x, y in
return x + (y.price * y.quantity)
}

print(total) // 13

Its the same principal, just a little more complicated. As long as your data structure is right it should be straight forward

Looping through CollectionView Cells in Swift

They way you're using as in that loop is trying to cast the array of visible cells to a single collection view cell. You want to cast to an array:

for cell in cv.visibleCells() as [UICollectionViewCell] {
// do something
}

or perhaps if you only have MyCollectionViewCell instances, this will work:

for cell in cv.visibleCells() as [MyCollectionViewCell] {
// do something
}

How to properly loop through cells of a table view and add the objects from the array to a new array

It looks like you did a problem a lot of new programmer do, globalize everything. _homePlayer looks like it should be a local variable. What I assume is happening is when the UITableView is populating itself by calling tableView:cellForRowAtIndexPath:, the last cell that will be generated will set the global _homePlayer. Then when you other loop function gets called, _homePlayer will have already been set, and you also never change it in your loop function. That's why you get the same 8 objects. Here's how to fix it:

Make step 3 this:

id homePlayer = _homePlayersArray[indexPath.row];
tableViewCell.textLabel.text = [NSString stringWithFormat:@"%d %@ %@", homePlayer.number, homePlayer.firstName, homePlayer.lastName];

You should replace 'id' with the homePlayer type so the compiler will assist you with auto completion.
I think you mentioned it was a NSDictionary, so replace id with NSDictionary *.

For your loop function do this: (comments for explanation)

// create a new and empty array
// (your local array will forever fill up with repeated objects unless emptied somewhere)
NSMutableArray *homeConfirmedPlayersArray = [[NSMutableArray alloc] init];

// loop through all indexpaths for the visible cells
for (NSIndexPath *indexPath in [self.homePlayers indexPathsForVisibleRows]){
// get the tablecell, only to check the accessory type
UITableViewCell *cell = [self.homePlayers cellForRowAtIndexPath:indexPath];

// I don't think this is necessary, especially if every cell has a checkmark
if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
// get the record from the home players array
NSDictionary *homePlayer = _homePlayersArray[indexPath.row];

// add to the copy
[homeConfirmedPlayersArray addObject:[NSString stringWithFormat:@"%d %@ %@",homePlayer.number,homePlayer.firstName,homePlayer.lastName]];
}
}

Remember to only use global variables only when you need them to be global.



Related Topics



Leave a reply



Submit