-1

Can you please help me to do this one?

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [popOveritem objectAtIndex:indexPath.row];
    [cell.textLabel setFont:[UIFont systemFontOfSize:10.05f]];
    cell.imageView.image = [UIImage imageNamed:[popimage objectAtIndex:indexPath.row]];
    NSLog(@"cellCOntent%@",cell.textLabel.text);
    UIView *cellbgColorView = [[[UIView alloc] init]autorelease];
    [cellbgColorView setBackgroundColor:[UIColor colorWithRed:40/255.0 green:128/255.0 blue:184/255.0 alpha:1.0]];
    cell.selectedBackgroundView = cellbgColorView;

    tableVIEW.scrollEnabled = NO;
    return cell;

first time the tableview will show one record and when i click to the button appeared in the view will add another row at the top of the table view?

4

1 回答 1

0

take a look at the below code hope it gives you what you need

viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *tblTest;

    NSMutableArray *arrData;

    int increment;
}

-(IBAction)onClickAdd:(id)sender;

@end

viewcontroller.m

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    arrData = [[NSMutableArray alloc] initWithObjects:@"Data 1", nil];
    increment = 1;
}

-(IBAction)onClickAdd:(id)sender
{
    increment++;
    NSString *strInsertData = [NSString stringWithFormat:@"Data %d",increment];
    [arrData insertObject:strInsertData atIndex:0];
    [tblTest reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arrData 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:nil];
        cell.accessoryView = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

    cell.textLabel.text = [arrData objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
于 2013-08-23T10:25:07.637 回答