Basically I’m making a list view that you can add things to the top of. The best way I can think of doing this is to store the UITableViewCells themselves in a NSMutableArray — Because I can simply pull them from the array them with all their data inside the object, and this list view will never be over 10 cells long.
Also note that I’m using Storyboards, hence the initWithCoder use.
The following code is what I’m trying, and it doesn’t work:
// This is where my NSMutableArray is initialized:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
if (!_CellsArray) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TestCell"];
_CellsArray = [NSMutableArray arrayWithObject:cell];
}
}
return self;
}
//UITableView Delegate & DataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TestCell"];
[_CellsArray insertObject:cell atIndex:0];
return [_CellsArray objectAtIndex:indexPath.row];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
I realize I may be approaching this in the wrong way, that’s why I’m here though 🙂
Thank you.
edit: fixed a type in the code (TimerCell -> UITableViewCell)
Let’s look at the order things get called in and what happens.
Your view controller is unarchived, so your
initWithCoder:method is called. This method creates a mutable array and puts one instance ofTimerCellinto it. Said instance is not further configured (unless you’ve overriddeninitWithStyle:reuseIdentifier:to do some configuration).Your data source method
tableView:numberOfRowsInSection:is called, and it tells the table view there are ten rows.Thus, your
tableView:cellForRowAtIndexPath:is called ten times. Each time, it creates a new instance ofUITableViewCelland inserts it into your mutable array. (After ten calls, your mutable array contains oneTimerCellat index 10 and tenUITableViewCells at indices 0-9.) It does nothing to configure the cell’s contents or appearance, then it returns the cell at the specified row index. On the first call, you’re asked for row 0, so the cell you just created and inserted at index 0 is returned. On the second call, you’re asked for row 1, so the cell at index 1 in your array is returned — since you just inserted a new cell at index 0, the cell you created on the last call has shifted to index 1, and you return it again. This continues with each call: you return the same unconfiguredUITableViewCellten times.It looks like you’re trying to out-think UIKit. This is almost never a good thing. (It’s been said that premature optimization is the root of all evil.)
UITableViewalready has a mechanism for cell reuse; it’s best to just keep track of your own cell content and let that mechanism do its thing. I took so long to type this that other answers have been written describing how to do that. Look to them, or to Apple’s documentation or any third-partyUITableViewtutorial.