Ios7 自定义单元格代替UIlabel怎么办?

Ios7 自定义单元格代替UIlabel怎么办?,ios7,Ios7,自定义单元格代替UIlabel我能做什么?在方法中,我希望使用自定义单元格,而不是标签。我想要自定义单元格,并且我是ios中的新成员或新成员,因此我不知道自定义单元格的更多细节,因此请告诉我如何在我的代码中使用自定义单元格代替tableview中的标签 //AppDelegate.h #import <UIKit/UIKit.h> @class ViewController; @interface AppDelegate : UIResponder <UIA

自定义单元格代替UIlabel我能做什么?在方法中,我希望使用自定义单元格,而不是标签。我想要自定义单元格,并且我是ios中的新成员或新成员,因此我不知道自定义单元格的更多细节,因此请告诉我如何在我的代码中使用自定义单元格代替tableview中的标签

//AppDelegate.h

    #import <UIKit/UIKit.h>

  @class ViewController;

  @interface AppDelegate : UIResponder <UIApplicationDelegate>

  @property (strong, nonatomic) UIWindow *window;

  @property (strong, nonatomic) ViewController *viewController;

  -(void)copydb;

  @end
//vc.h

  #import <UIKit/UIKit.h>
  #import <sqlite3.h>
  @interface ViewController :
                               UIViewController<UITextFieldDelegate,UIAlertViewDelegate,UITableViewDataSource,UITableViewD      elegate>
  {
IBOutlet UITextField *fnameTxt;
IBOutlet UITextField *lnameTxt;
IBOutlet UITextField *addressTxt;
IBOutlet UITextField *birthdateTxt;
IBOutlet UITextField *cityTxt;
IBOutlet UITableView *tbl;

sqlite3 *db;
NSMutableArray *data;
  }

  @property (atomic,retain)UILabel *label1;
  @property (atomic,retain)UILabel *label2;
  @property (atomic,retain)UILabel *label3;
  @property (atomic,retain)UILabel *label4;
  @property (atomic,retain)UILabel *label5;

  - (IBAction)submit:(id)sender;
  - (IBAction)update:(id)sender;
  - (IBAction)deletes:(id)sender;

  @end

请查看我上面的代码,只需告诉我有关自定义单元格的信息。

在您的
cellforrowatinexpath
的表视图方法中。您需要用自定义的
uitableviewcell
类替换此代码。然后在这个文件中,您需要创建nib文件并在
cellforrowatinexpath
方法中加载

//注释此代码

 //   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
//用下面提到的链接代码修改相同的上述代码

请参阅您说的“…自定义单元格代替UIlabel我能做什么?”

根本不清楚你在问什么。表视图单元格仅位于表视图内部,并且表视图不能包含UILabel来代替表视图单元格。它们永远不能互换

你发布了很多代码,大部分都是标准的锅炉铭牌代码。我费力地完成了这一切(真烦人…)

在cellForRowAtIndexPath方法中,您正在创建一个标准UITableViewCell并向其添加5个UILabel。在我看来,标签太大,无法放入单元格,但这是次要的

您想做什么,创建UITableViewCell的自定义子类,其中包含作为IBOutlet的标签,并实例化自定义单元格而不是标准单元格

请清楚地说明你想做什么

此外,您还需要告诉我们您是否正在使用故事板或XIB文件,以及您是否正在使用UITableViewController或标准视图控制器

  #import "ViewController.h"

  @interface ViewController ()

  @end

  @implementation ViewController

  @synthesize label1;
  @synthesize label2;
  @synthesize label3;
        @synthesize label4;
  @synthesize label5;

  - (void)viewDidLoad
  {
      [super viewDidLoad];

      data=[[NSMutableArray alloc]init];

      [self load];

  // Do any additional setup after loading the view, typically from a nib.

  }

  -(void)load
  {

[fnameTxt resignFirstResponder];
[lnameTxt resignFirstResponder];
[addressTxt resignFirstResponder];
[birthdateTxt resignFirstResponder];
[cityTxt resignFirstResponder];

fnameTxt.text=@"";
lnameTxt.text=@"";
addressTxt.text=@"";
birthdateTxt.text=@"";
cityTxt.text=@"";

[data removeAllObjects];

NSString *insertstatement=[NSString stringWithFormat:@"select * from
  emps"];
NSLog(@"%@",insertstatement);
sqlite3_stmt *statement;
NSString *path=[self getDBPath];
NSLog(@"%@",path);

if (sqlite3_open([path UTF8String], &db)==SQLITE_OK)
      {
   // if (sqlite3_prepare_v2(db, [insertstatement UTF8String], -1,
  &statement, NULL)==SQLITE_OK)
    if (sqlite3_prepare_v2(db, [insertstatement UTF8String], -1,
  &statement, NULL)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW)
        {
            NSMutableDictionary *record=[[NSMutableDictionary
  alloc]init];

            NSString *fnm=[NSString stringWithUTF8String:(char
  *)sqlite3_column_text(statement, 0)];
            NSString *lnm=[NSString stringWithUTF8String:(char
  *)sqlite3_column_text(statement, 1)];
            NSString *addr=[NSString stringWithUTF8String:(char
  *)sqlite3_column_text(statement, 2)];
            NSString *bDay=[NSString stringWithUTF8String:(char
  *)sqlite3_column_text(statement, 3)];
            NSString *cty=[NSString stringWithUTF8String:(char
  *)sqlite3_column_text(statement, 4)];

            [record setValue:fnm forKey:@"fName"];

            [record setValue:lnm forKey:@"lName"];

            [record setValue:addr forKey:@"adds"];

            [record setValue:bDay forKey:@"birthDay"];

            [record setValue:cty forKey:@"city"];

            [data addObject:record];

        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(db);
      }
      NSLog(@"Number of data in Array is = %d",[data count]);
      [tbl reloadData];

  }


  - (BOOL)textFieldShouldReturn:(UITextField *)textField
  {
[fnameTxt resignFirstResponder];
[lnameTxt resignFirstResponder];
[addressTxt resignFirstResponder];
[birthdateTxt resignFirstResponder];
[cityTxt resignFirstResponder];

return YES;
  }


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

  - (NSInteger)tableView:(UITableView *)tableView
  numberOfRowsInSection:(NSInteger)section
  {
 return [data count];
  }





      - (UITableViewCell *)tableView:(UITableView *)tableView
       cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
        static NSString *cellIdentifier=@"cell";

        UITableViewCell *cell=[tableView
         dequeueReusableCellWithIdentifier:cellIdentifier];

       if (cell==nil)
      {
    cell=[[UITableViewCell
    alloc]initWithStyle:UITableViewCellStyleValue1
         reuseIdentifier:cellIdentifier];

    //my added new code

    label1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 3, 100, 40)];
    label1.tag=1;
    label1.font = [UIFont systemFontOfSize: 20.0];
    label1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleHeight;
    label1.backgroundColor = [UIColor clearColor];
    label1.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label1];

    label2 = [[UILabel alloc]initWithFrame:CGRectMake(80, 3, 100, 40)];
    label2.tag=2;
    label2.font = [UIFont systemFontOfSize: 20.0];
    label2.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
       UIViewAutoresizingFlexibleHeight;
    label2.backgroundColor = [UIColor clearColor];
    label2.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label2];


    label3 = [[UILabel alloc]initWithFrame:CGRectMake(155, 3, 80, 40)];
    label3.tag=3;
    label3.font = [UIFont systemFontOfSize: 20.0];
    label3.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleHeight;
    label3.backgroundColor = [UIColor clearColor];
    label3.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label3];


    label4 = [[UILabel alloc]initWithFrame:CGRectMake(230, 3, 100, 40)];
    label4.tag=4;
    label4.font = [UIFont systemFontOfSize: 20.0];
    label4.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleHeight;
    label4.backgroundColor = [UIColor clearColor];
    label4.textColor = [UIColor blackColor];
    [cell.contentView addSubview:label4];


    label5= [[UILabel alloc]initWithFrame:CGRectMake(320, 3, 120, 40)];
    label5.tag=5;
    label5.font = [UIFont systemFontOfSize: 20.0];
    label5.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleHeight;
    label5.backgroundColor=[UIColor clearColor];
    label5.textColor=[UIColor blackColor];
    [cell.contentView addSubview:label5];

    }
    else
    {
    label1 = (UILabel*)[cell.contentView viewWithTag:1];
    label2 = (UILabel*)[cell.contentView viewWithTag:2];
    label3 = (UILabel*)[cell.contentView viewWithTag:3];
    label4 = (UILabel*)[cell.contentView viewWithTag:4];
    label5 = (UILabel*)[cell.contentView viewWithTag:5];

    }


    label1.text=[[data objectAtIndex:indexPath.row]valueForKey:@"fName"];
    label2.text=[[data objectAtIndex:indexPath.row]valueForKey:@"lName"];
    label3.text=[[data objectAtIndex:indexPath.row]valueForKey:@"adds"];
    label4.text=[[data objectAtIndex:indexPath.row]valueForKey:@"birthDay"];
     label5.text=[[data objectAtIndex:indexPath.row]valueForKey:@"city"];
   /*
    [cell addSubview:label1];
    [cell addSubview:label2];
    [cell addSubview:label3];
     [cell addSubview:label4];
    [cell addSubview:label5];
    */


   /*    [cell.contentView addSubview:label1];
   [cell.contentView addSubview:label2];
   [cell.contentView addSubview:label3];
    [cell.contentView addSubview:label4];
    [cell.contentView addSubview:label5];
    */


      /*   cell.textLabel.text=[[data
  objectAtIndex:indexPath.row]valueForKey:@"fName"];
 cell.detailTextLabel.text=[[data
  objectAtIndex:indexPath.row]valueForKey:@"lName"];
 cell.detailTextLabel.text=[[data
        objectAtIndex:indexPath.row]valueForKey:@"adds"];
 cell.detailTextLabel.text=[[data
  objectAtIndex:indexPath.row]valueForKey:@"birthDay"];
 cell.detailTextLabel.text=[[data
  objectAtIndex:indexPath.row]valueForKey:@"city"];
 */
return cell;

  }

  - (void)tableView:(UITableView *)tableView
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
fnameTxt.text=[[data objectAtIndex:indexPath.row] valueForKey:@"fName"];
lnameTxt.text=[[data objectAtIndex:indexPath.row] valueForKey:@"lName"];
addressTxt.text=[[data objectAtIndex:indexPath.row] valueForKey:@
  "adds"];
birthdateTxt.text=[[data objectAtIndex:indexPath.row] valueForKey:@
  "birthDay"];
cityTxt.text=[[data objectAtIndex:indexPath.row] valueForKey:@"city"];

  }


  - (IBAction)submit:(id)sender
  {
      if([fnameTxt.text isEqualToString:@""] || [lnameTxt.text
  isEqualToString:@""] || [addressTxt.text isEqualToString:@""] ||
  [birthdateTxt.text isEqualToString:@""] || [cityTxt.text isEqualToString:@
  ""])
      {
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"SORRY ERROR
  !!!" message:@"Please Don't leaves any blanlk detail...." delegate:nil
  cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
      }
      else
      {

    NSString *path=[self getDBPath];
    NSLog(@"%@",path);
    NSString *insertstatement=[NSString stringWithFormat:@"insert into
  emps (fName,lName,adds,birthDay,city) values
              ('%@','%@','%@','%@','%@')",fnameTxt.text,lnameTxt.text,addressTxt.text,birthdateTxt.text,cityTxt.text];

          NSLog(@"%@",insertstatement);

    if (sqlite3_open([path UTF8String], &db)==SQLITE_OK)
    {
        //        char *errMsg;
       //        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS imageTbl (id INTEGER PRIMARY KEY AUTOINCREMENT, imagestr TEXT)";
      //        
      //        if (sqlite3_exec(db, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
      //        {
      //            //status.text = @"Failed to create table";
      //            NSLog(@"Failed to create table");
      //        }
      //        else
      //        {
      //            NSLog(@"create table Successfully");
      //        }
        const char *sql=[insertstatement UTF8String];
        char *err;

        if(sqlite3_exec(db, sql, NULL, NULL, &err)!=SQLITE_OK)
        {
            NSLog(@"Error in insert !!!");
        }
        else
        {
            NSLog(@"Insert perform successfully!!!");
            UIAlertView *alert=[[UIAlertView
  alloc]initWithTitle:@"Submited..."
  message:@"Record submited successfully....." delegate:nil
  cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        sqlite3_close(db);
    }
    else
    {
    }
    [self load];
    /*
     *** code for display the textbox data into alert view ***
     UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"user
  Information" message:[NSString stringWithFormat:@"First Name :%@ \n Last
  Name :%@ \n Address :%@ \n BirthDate :%@ \n City : %@",fnameTxt.text ,
  lnameTxt.text ,addressTxt.text , birthdateTxt.text , cityTxt.text ]
   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

     [aler show];
     */
            }
  }

  - (IBAction)update:(id)sender
  {
      NSString *path=[self getDBPath];

      NSString *insertstatement=[NSString stringWithFormat:@"update emps set
  fName='%@',lName='%@',adds='%@',birthDay='%@',city='%@' where
  birthDay='%@'"
  ,fnameTxt.text,lnameTxt.text,addressTxt.text,birthdateTxt.text,cityTxt.text,birthdateTxt.te      xt];

      NSLog(@"%@",insertstatement);

      if (sqlite3_open([path UTF8String], &db)==SQLITE_OK)
      {
    const char *sql=[insertstatement UTF8String];
    char *err;

    if(sqlite3_exec(db, sql, NULL, NULL, &err)!=SQLITE_OK)
    {
        NSLog(@"Error in Update !!!");
    }
    else
    {
        NSLog(@"Update perform successfully!!!");
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Update..."
  message:@"Record Updated successfully....." delegate:nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil, nil];
        [alert show];
    }
    sqlite3_close(db);
      }
      else
      {
      }
      [self load];

  }

        - (IBAction)deletes:(id)sender
  {
      UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Are
  you sure to DELETE this record ???" delegate:self cancelButtonTitle:@"Cancel"
  otherButtonTitles:@"OK", nil];
[alert show];

      /*
       ***this code written into alert OK button click event ***

       NSString *path=[self getDBPath];

       NSString *insertstatement=[NSString stringWithFormat:@" delete from
         emp  where birthDay='%@' " , birthdateTxt.text];

       NSLog(@"%@",insertstatement);

       if (sqlite3_open([path UTF8String], &db)==SQLITE_OK)
       {
       const char *sql=[insertstatement UTF8String];
       char *err;

 if(sqlite3_exec(db, sql, NULL, NULL, &err)!=SQLITE_OK)
 {
 NSLog(@"Error in Delete !!!");
 }
 else
 {
 NSLog(@"Delete perform successfully!!!");
 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Delete..."
  message:@"Record Deleted successfully....." delegate:nil
  cancelButtonTitle:@"OK"
  otherButtonTitles:nil, nil];
 [alert show];
 }
 sqlite3_close(db);
 }
 else
 {
 }
 [self load];

 */
  }


  // *** code for alertview button click ***

  - (void)alertView:(UIAlertView *)alertView
  clickedButtonAtIndex:(NSInteger)buttonIndex
  {
if (buttonIndex == 0)
{

}
if (buttonIndex == 1)
{
    NSString *path=[self getDBPath];

    NSString *insertstatement=[NSString stringWithFormat:@" delete from
    emps  where birthDay='%@' " , birthdateTxt.text];

    NSLog(@"%@",insertstatement);

    if (sqlite3_open([path UTF8String], &db)==SQLITE_OK)
    {
        const char *sql=[insertstatement UTF8String];
        char *err;

        if(sqlite3_exec(db, sql, NULL, NULL, &err)!=SQLITE_OK)
        {
            NSLog(@"Error in Delete !!!");
        }
        else
        {
            NSLog(@"Delete perform successfully!!!");
            UIAlertView *alert=[[UIAlertView
  alloc]initWithTitle:@"Delete..."
  message:@"Record Deleted successfully....." delegate:nil
  cancelButtonTitle:@"OK"
  otherButtonTitles:nil, nil];
            [alert show];
        }
        sqlite3_close(db);
    }
    else
    {
    }
    [self load];

      }

  }


  -(NSString *)getDBPath
  {
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  NSUserDomainMask, YES);
NSString *documentdirectory=[paths objectAtIndex:0];
NSString *inspath=[NSString stringWithFormat:@"sample.sqlite"];
NSString *destpath=[documentdirectory
  stringByAppendingPathComponent:inspath];

return destpath;
  }

  - (void)didReceiveMemoryWarning
  {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
  }
  @end
 //   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];