2

如何在 in 中显示整UILabel数值ViewDidLoad?我为文本、日期和图像做了,但如何将 int 转换为字符串。我正在尝试使用以下代码

 NSString* label=[aa stringWithFormat:@"%d",((Comments *) [self.list objectAtIndex:0]).noofcomm]]; 
 [self.comments2 setText:label];

但没用。请帮帮我。如何用 UILabel 设置整数?

这是我的评论.h

    @interface Comments : NSObject
{
    NSInteger iD;
    UIImage *photo;
    NSString *name;
    NSString *descrp;
    NSDate *date;
    NSString *msg;
    NSInteger noofcomm;
    NSInteger nooflikes;
}
@property(nonatomic,assign)NSInteger iD;
@property(nonatomic,retain)UIImage *photo;
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *descrp;
@property(nonatomic,strong)NSDate *date;
@property(nonatomic,retain)NSString *msg;
@property(nonatomic,assign)NSInteger noofcomm;
@property(nonatomic,assign)NSInteger nooflikes;


@end

数据库类.m

     #import "DBClass.h"
#import "Comments.h"

@implementation DBClass
- (NSMutableArray *) getMyComments{
    NSMutableArray *wineArray = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"ComntDB.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured.");
        }
        const char *sql = "SELECT id, photo,name,descrp, time,msg,comments,likes FROM Com";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement");
        }

        //
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            Comments *MyWine = [[Comments alloc]init];

            MyWine.iD = sqlite3_column_int(sqlStatement, 0);

            const char *raw = sqlite3_column_blob(sqlStatement, 1);
            int rawLen = sqlite3_column_bytes(sqlStatement, 1);
            NSData *data = [NSData dataWithBytes:raw length:rawLen];
            MyWine.photo = [[UIImage alloc] initWithData:data];
            MyWine.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];

            MyWine.descrp = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];

            MyWine.date=[NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(sqlStatement,4)];
             MyWine.msg = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,5)];
            MyWine.noofcomm = sqlite3_column_int(sqlStatement, 6);
            MyWine.nooflikes = sqlite3_column_int(sqlStatement, 7);

            [wineArray addObject:MyWine];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"An exception occured: %@", [exception reason]);
    }
    @finally {
        return wineArray;
    }

}

@end

根视图控制器.m

    #import "RootViewController.h"
#import "Comments.h"
#import "DBClass.h"

@interface RootViewController ()

@end

@implementation RootViewController
@synthesize list;
@synthesize image2;
@synthesize name2;
@synthesize descrp2;
@synthesize msg2;
@synthesize date2;
@synthesize comments2;
@synthesize likes2;

- (void)viewDidLoad
{
    DBClass * mywines =[[DBClass alloc] init];
    self.list = [mywines getMyComments];
    [self.image2 setImage:((Comments *) [self.list objectAtIndex:0]).photo];
    [self.name2 setText:((Comments *) [self.list objectAtIndex:0]).name];

    [self.descrp2 setText:((Comments *) [self.list objectAtIndex:0]).descrp];

    NSDateFormatter* fmtr = [[NSDateFormatter alloc] init];
    [fmtr setDateFormat:@"MM/dd/yy"];
    NSString* label_str = [fmtr stringFromDate:((Comments *) [self.list objectAtIndex:0]).date];  
[self.date2 setText:label_str];
          [self.msg2 setText:((Comments *) [self.list objectAtIndex:0]).msg];
    //[self.comments2 setText:((Comments *) [self.list objectAtIndex:0]).noofcomm];
       // int solution = 1;
//    [self.comments2 setText:[NSString stringWithFormat:@"%d", solution]];
//    int solution2 = 1;
//    [self.likes2 setText:[NSString stringWithFormat:@"%d", solution2]];

    [super viewDidLoad];
}
- (void)viewDidUnload
{
    [self setImage2:nil];
    [self setName2:nil];
    [self setMsg2:nil];
    [self setDescrp2:nil];
    [self setComments2:nil];
    [self setLikes2:nil];
    [self setDate2:nil];
    [super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

4 回答 4

2
NSInteger someInteger = myInteger;
NSString *someString = [NSString stringWithFormat:@"%d", someInteger];
myLabel.text = someString;

或者

NSNumber *someNumber = @(myInteger);
NSString *someString = [someNumber stringValue];
myLabel.text = someString;

两者都会起作用。

编辑:

在您的情况下,它将是这样的:

NSInteger someInteger = ((Comments *) [self.list objectAtIndex:0]).noofcomm;
NSString someString = [NSString stringWithFormat:@"%d", someInteger];
self.comments2.text = someString;

如果它仍然不起作用,那么肯定问题出在其他地方,而不是转换。检查属性noofcomm是否具有有效值,检查您的标签引用是否正常(在转换前使用随机值进行测试)等等。

于 2013-08-18T15:44:10.670 回答
1

您需要构建一个 NSString

int someInteger = 10;
NSString *someString = [[NSString alloc] initWithFormat:@"%d", someInteger];
于 2013-08-18T15:38:17.710 回答
0

从 0.1.3 开始,您可以在 LCategory 中使用 [NSString string_from_int:42] 之类的东西:https ://github.com/superarts/LCategory

于 2014-06-06T05:02:46.407 回答
-1
_lbl_yourLabel.text=[NSString stringWithFormat:@"%d",[[dic valueForKey:@"your integer value"] intValue]];

左上角是您的标签,名为“yourLabel”,“dic”是您的 json 响应字典,其中所有数据都以键值形式出现,“您的整数值”是将值分配给标签的键“ yourLabel”,我们采用了 intValue,因为我们不能将整数值直接分配给标签。

或者您也可以尝试以下方法:

int anyInteger = 13;
NSString *yourString = [[NSString alloc] initWithFormat:@"%d", anyInteger];
self.yourLabel.text = yourString;
于 2016-12-09T10:57:20.773 回答