我们如何在每次在桌面视图顶部的第一行添加一行按钮单击视图?

时间:2013-08-23 08:24:54

标签: iphone

你能帮我做这个吗?

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;

第一次tableview将显示一条记录,当我点击视图中出现的按钮时,会在表格视图的顶部添加另一行吗?

1 个答案:

答案 0 :(得分:0)

看看下面的代码希望它能为您提供所需的信息

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];
}