iphone - Table View Programming -
Why can not I add a subview in the content view of the cell to see the table? (UITableViewCell *) Table View: (UITableView *) Table View CellForAindind Peth: (NSINXAP *) Index Path {Fixed NSString * CellTable Identifier = "" CellTable Identifier "; UITableViewCell * cell = [TablesView Reuse Eligible cell WthiDientifier: cellTable identifier]; If (cell == blue) {cell = [[UITLL VILL] in with style: UITBel VivlCellTable default reuse identifier: cellTable identifier]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 5, 70, 15)]; Label.text = @ "name:"; [Add Cell.contentView Subview: Labels]; } Return [Cell Autoreige]; }
This should be a memory management problem.
Consider this statement:
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellTable identifier]; According to
, this is not a alloc
/ new
/ copy
method, that means you is not a cell
, so you should use it after you not -rele
complete it, however, the next statement (value cell! = Void
):
back [cell auto -quare];
The release will release (after some time) to cell
, causing the cell to be released twice (after some time). So you just need a return cell;
.
Now what if cell == zero
? If
inside the branch, you have written
cell = [[UTABL power ALL] Init with style: UITABLE VISSEAL Default Rewad Identifier: cellTable identifier]; Since cell
is built with alloc
, you are responsible for the -release
. But since you are going to return it to others, since then you have to give up its ownership, while it will not be canceled immediately. This is the place where -autorelease
is used, Temporaryly a proprietorship is transferred to "AutoCouge Pool" so that another code can be given the chance to be redirected to ownership (before -retain
)
In other words You can call this statement to cell = [[[...] au Must replace with torelease;
.
Similarly, you should have -release
as label
because it is -alloc
and you are no longer yourself, however, You do not need -autorelease
because you are certain that there is already another owner ( cell.contentView
).
In essence, you should rewrite this code:
Fixed NSString * cellTable identifier = @ "celltable identifier"; UITableViewCell * cell = [TablesView Reuse Eligible cell WthiDientifier: cellTable identifier]; If (cell == blue) {cell = [[[UTTViewjell Alok] Init with style: UITTualWeelSteel Default ReuseEditFire: CellTable Identifier] AutoEques]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 5, 70, 15)]; Label.text = @ "name:"; [Add Cell.contentView Subview: Labels]; [Label release]; } Return cell;
In BTW, you can use the UITableViewCellStyleValue2
(like the address book) to avoid mess with the hierarchy.
Comments
Post a Comment