Uialerttableview. h
# Import <Uikit/uikit. h>
@ ClassUialertview;
@ InterfaceUialerttableview: uialertview {
Uialertview * alertview;
Uitableview * tableview;
IntTableheight;
IntTableextheight;
ID<Uitableviewdatasource> datasource;
ID<Uitableviewdelegate> tabledelegate;
}
@ Property (nonatomic, assign)IDDatasource;
@ Property (nonatomic, assign)IDTabledelegate;
@ Property (nonatomic,Readonly) Uitableview * tableview;
@ Property (nonatomic, assign)IntTableheight;
-(Void) Prepare;
@ End
Uialerttableview. m
# Import " Uialerttableview. h "
# DefineKtablepadding 8.0f
@ InterfaceUialertview (Private)
-(Void) Layoutanimated :( bool) FP;
@ End
@ ImplementationUialerttableview
@ SynthesizeDatasource;
@ SynthesizeTabledelegate;
@ SynthesizeTableheight;
@ SynthesizeTableview;
-( Void ) Layoutanimated :( bool) FP {
[Super layoutanimated: FP];
[Self setframe: cgrectmake (self. Frame. Origin. X, self. Frame. Origin. Y-tableextheight/ 2 , Self. Frame. Size. Width, self. Frame. Size. height + tableextheight)];
Uiview * lowestview;
Int I = 0 ;
While (! [[Self. subviews objectatindex: I] iskindofclass: [uicontrol Class ]) {
Lowestview = [self. subviews objectatindex: I];
I ++;
}
Cgfloat tablewidth = 262.0f ;
Tableview. Frame = cgrectmake ( 11.0f , Lowestview. Frame. Origin. Y + lowestview. Frame. Size. height + 2 * Ktablepadding, tablewidth, tableheight );
For (Uiview * V In Self. subviews ){
If ([V iskindofclass: [uicontrol Class ]) {
V. frame = cgrectmake (v. frame. origin. x, V. frame. origin. Y + tableextheight, V. frame. size. width, V. frame. size. height );
}
}
}
-(Void) Show {
[Self prepare];
[Super Show];
}
-( Void ) Prepare {
If (Tableheight = 0 ){
Tableheight = 150366f ;
}
Tableextheight = tableheight + 2 * Ktablepadding;
Tableview = [[uitableview alloc] initwithframe: cgrectmake ( 0.0f , 0.0f , 0.0f , 0.0f ) Style: uitableviewstyleplain];
Tableview. backgroundcolor = [uicolor orangecolor];
Tableview. Delegate = Tabledelegate;
Tableview. datasource = datasource;
[Self insertsubview: tableview atindex: 0 ];
[Self setneedslayout];
}
-(Void) Dealloc {
[Tableview release];
[Super dealloc];
}
@ End
Alerttableviewcontroller. h
# Import <Uikit/uikit. h>
@ InterfaceAlerttableviewcontroller: uiviewcontroller <uialertviewdelegate, uitableviewdelegate, uitableviewdatasource> {
Uitableview * mytableview;
Nsarray * array;
Nsindexpath * lastindexpath;
}
@ Property (nonatomic, retain) nsindexpath * lastindexpath;
-(Ibaction) btnclick: sender;
@ End
Alerttableviewcontroller. m
# Import " Alerttableviewcontroller. h "
# Import " Uialerttableview. h "
@ ImplementationAlerttableviewcontroller
@ SynthesizeLastindexpath;
-(Void) Viewdidload {
Array = [[nsarray alloc] initwithobjects:@"Test1",@"Test2",@"Test3",@"Test4", Nil];
[Super viewdidload];
}
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {
Return[Array count];
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {
Static Nsstring * cellidentifier = @" Cell " ;
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
If (Cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autorelease];
}
Nsuinteger ROW = [indexpath row];
Nsuinteger oldrow = [lastindexpath row];
Cell. textlabel. Text = [array objectatindex: Row];
Cell. accessorytype = (ROW = oldrow & lastindexpath! = Nil )? Uitableviewcellaccessorycheckmark: uitableviewcellaccessorynone;
Return Cell;
}
-(Nsindexpath *) tableview :( uitableview *) tableview willselectrowatindexpath :( nsindexpath *) indexpath {
ReturnIndexpath;
}
-(Cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {
Return 40;
}
-( Void ) Tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {
Int Newrow = [indexpath row];
Int Oldrow = [lastindexpath row];
If (Newrow = 0 & Oldrow = 0 ) | (Newrow! = Oldrow )){
Uitableviewcell * Newcell = [tableview cellforrowatindexpath: indexpath];
Newcell. accessorytype = uitableviewcellaccessorycheckmark;
Uitableviewcell * oldcell = [tableview cellforrowatindexpath: lastindexpath];
Oldcell. accessorytype = uitableviewcellaccessorynone;
Lastindexpath = [indexpath retain];
}
[Tableview deselectrowatindexpath: indexpath animated: Yes];
}
-(Ibaction) btnclick: sender
{
Uialerttableview * Alert = [[uialerttableview alloc] initwithtitle: @" Select option "
Message: Nil
Delegate : Self
Cancelbuttontitle: @" Cancel "
Otherbuttontitles: @" Do " , Nil];
Alert. tabledelegate = self;
Alert. datasource = self;
Alert. tableheight = 120 ;
[Alert show];
[Alert release];
}
-(Void) Dealloc {
[Lastindexpath release];
[Array release];
[Super dealloc];
}
@ End
Example: