基于单元格数据的iPhone表格视图单元格样式
我试图找出如何根据单元格的文本内容在单元格中显示特定类型的图像。例如,我正在使用的教程中的此方法将允许我显示前3个单元格的星形图像:基于单元格数据的iPhone表格视图单元格样式,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我试图找出如何根据单元格的文本内容在单元格中显示特定类型的图像。例如,我正在使用的教程中的此方法将允许我显示前3个单元格的星形图像: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if (row < 3) {
UIImage *image = [UIImage imageNamed:@"star.png"];
cell.imageView.image = image;
}
cell.textLabel.text = [listData objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
return cell;
}
-(UITableViewCell*)表视图:(UITableView*)表视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*SimpleTableIdentifier=@“SimpleTableIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
如果(单元格==nil){
单元格=[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier]自动释放];
}
NSUTEGER行=[indexPath行];
如果(第3行){
UIImage*image=[UIImage ImageName:@“star.png”];
cell.imageView.image=图像;
}
cell.textlab.text=[listData objectAtIndex:row];
cell.textlab.font=[UIFont boldSystemFontOfSize:15];
返回单元;
}
有什么办法可以在这里得到实际的单元格内容吗?因此,例如,如果单元格包含文本“bob”,我可以显示不同的图像而不是星形?也许您应该设置一个字典,其中包含人名作为字典的键,图像名作为值。然后,您可以获取单元格的文本并将其用作字典的查找。设置文本后:
if([cell.textLabel.text rangeOfString:@"bob"].location != NSNotFound) {
UIImage *image = [UIImage imageNamed:@"bob.png"];
cell.imageView.image = image;
}
else {
UIImage *image = [UIImage imageNamed:@"star.png"];
cell.imageView.image = image;
}
看起来你可以看看你的
listData
对象,然后在那里决定。你可以试试这样的
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if ([[listData objectAtIndex:row] isEqualToString:@"bob"]) {
UIImage *image = [UIImage imageNamed:@"bob.png"];
cell.imageView.image = image;
}
else if (row < 3) {
UIImage *image = [UIImage imageNamed:@"star.png"];
cell.imageView.image = image;
}
cell.textLabel.text = [listData objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
return cell;
}
-(UITableViewCell*)表视图:(UITableView*)表视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*SimpleTableIdentifier=@“SimpleTableIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
如果(单元格==nil){
单元格=[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier]自动释放];
}
NSUTEGER行=[indexPath行];
if([[listData objectAtIndex:row]isEqualToString:@“bob”]){
UIImage*image=[UIImage ImageName:@“bob.png”];
cell.imageView.image=图像;
}
否则,如果(第3行){
UIImage*image=[UIImage ImageName:@“star.png”];
cell.imageView.image=图像;
}
cell.textlab.text=[listData objectAtIndex:row];
cell.textlab.font=[UIFont boldSystemFontOfSize:15];
返回单元;
}
谢谢,由于我对iOS有点陌生,我完全忽略了listData对象索引:行