如何从Objective c中的UITableView获取UILabel的多个值
Posted
技术标签:
【中文标题】如何从Objective c中的UITableView获取UILabel的多个值【英文标题】:How to get multiple value on UILabel from UITableView in Objective c 【发布时间】:2016-11-12 06:21:07 【问题描述】:我是 ios 新手,我面临着从 UITableView 获取 UILabel 中的多个值的问题。我的代码是这样的
#pragma mark - Complaints Assigned Connection Method
//Connection Method and Delegate...
-(void)serverconnection
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<Method xmlns=\"http://tempuri.org/\">"
"</Method>"
"</soap:Body>"
"</soap:Envelope>"];
NSURL *myNSUObj=[NSURL URLWithString:@"URL"];
// NSURLRequest *myNSURequestObj=[NSURLRequest requestWithURL:myNSUObj];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myNSUObj];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/Method" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
myNSUConnectionassignedObj=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
NSLog(@"Data =%@",myNSUConnectionassignedObj);
if(myNSUConnectionassignedObj)
NSLog(@"successful connection");
myNSMDataassignedFromServer=[[NSMutableData alloc]init];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
[myNSMDataassignedFromServer setLength:0];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[myNSMDataassignedFromServer appendData:data];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSXMLParser *myNSXMLParserassignedObj=[[NSXMLParser alloc]initWithData:myNSMDataassignedFromServer];
myNSXMLParserassignedObj.delegate=self;
[myNSXMLParserassignedObj parse];
NSLog(@"%@",myNSXMLParserassignedObj.parserError);
idassignedarray=[[NSMutableArray alloc]init];
if(myNSUConnectionassignedObj == nil)
assignedtable.hidden=YES;
idassignedarray=[responseassigneddict valueForKey:@"abc"];
NSLog(@"Result Array =%@",idassignedarray);
// [myDataNSMArray addObject:idassignedarray];
shortnameassignedarray=[[NSMutableArray alloc]init];
shortnameassignedarray=[responseassigneddict valueForKey:@"abcid"];
[assignedtable reloadData];
#pragma mark - NSXMLParsing Delegate
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
if([elementName isEqualToString:@"FillBlocksNew"])
myDataClassObj=[[mydata alloc]init];
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
myMutableStringassignedObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringassignedObj);
NSData *data = [myMutableStringassignedObj dataUsingEncoding:NSUTF8StringEncoding];
responseassigneddict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON DATA = %@",responseassigneddict);
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
NSLog(@"DataArray: %@",myDataNSMArray);
#pragma mark - TableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [idassignedarray count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *STI=@"STI";
AssignTableViewCell *cell = (AssignTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AssignTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
cell.newlbl.text=[idassignedarray objectAtIndex:indexPath.row];
return cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[[assignedtable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
activityid.text=[NSString stringWithFormat:@"%@",[shortnameassignedarray objectAtIndex:indexPath.row]];
它的输出是这样的
在 UILabel 我只得到一个值。第一个值被覆盖。如何在 UILabel 中获取多个选定的 id。提前致谢!
我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *STI=@"STI";
AssignTableViewCell *cell = (AssignTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AssignTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
cell.newlbl.text = [idassignedarray objectAtIndex:indexPath.row];
if ([cellSelected containsObject:cell.newlbl.text])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[[assignedtable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
activityid.text=[NSString stringWithFormat:@"%@",[shortnameassignedarray objectAtIndex:indexPath.row]];
activitytxt.text=[idassignedarray objectAtIndex:indexPath.row];
static NSString *STI=@"STI";
AssignTableViewCell *cell = (AssignTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AssignTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
if ([cellSelected containsObject:cell.newlbl.text])
[cellSelected removeObject:cell.newlbl.text];
NSString *str = [cellSelected componentsJoinedByString: @","];
NSLog(@"String value is =%@",str);
else
[cellSelected addObject:cell.newlbl.text];
NSString *str = [cellSelected componentsJoinedByString: @","];
NSLog(@"String value is =%@",str);
【问题讨论】:
所以您需要创建所选单元格字符串的数组? 当你想在标签上显示选定的值时?有按钮吗?以及如何在标签上显示多个值?我的意思是逗号分隔?还是别的什么? @Lion 是的,它应该在 UILabel 上用逗号分隔。在 UITableView 的 Didselected 方法中。 您需要选定单元格的逗号分隔值吗? @vaibhav 是的,我需要。 【参考方案1】:你的didselectRowAtIndexPath
应该是这样的,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[[assignedtable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
NSArray *indexPathForSelectedRow = [assignedtable indexPathsForSelectedRows];
NSString *str = [[NSString alloc]init];
for (int i = 0; i < indexPathForSelectedRow.count; i++)
NSIndexPath *iPath = [indexPathForSelectedRow objectAtIndex:i];
[str stringByAppendingString:[NSString stringWithFormat:@"%@,",[shortnameassignedarray objectAtIndex:iPath.row]]];
if (str.length > 0)
str = [str substringToIndex:[str length] - 1];
activityid.text= str;
并确保为您的tableview
启用多项选择!!
【讨论】:
我在标签上一无所获。当我滚动 UITableView 选择被删除 在 viewDidload 中设置assignedtable.allowsMultipleSelection = YES;
然后试试!
在 str 中显示“对象返回空描述”
确保shortnameassignedarray
有字符串对象并且它不为零,并确保您在indexPathForSelectedRow
数组中获取索引路径!并采取新的更新答案!!!!这是一个小错误!
"indexPathForSelectedRow 的打印说明:<__nsarrayi>( 你可以通过两种方式做到这一点。
1) 将所有文本内容附加到一个 larde NSString 中。您可以将其分配给文本标签。通过将行数设置为 0,可以使用大字符串。
2) 另一种方式,可能是您想要的,是更改表格中的单元格。通过更改或创建新的单元格类型。这允许您在一个单元格中添加更多 UILabel(和其他类型)。
【讨论】:
以上是关于如何从Objective c中的UITableView获取UILabel的多个值的主要内容,如果未能解决你的问题,请参考以下文章
从Objective C中的NSArray转换时如何比较String?
如何从 Objective C、iOS 中的 NSUserDefaults 获取准确数据?
如何从Objective c中的UITableView获取UILabel的多个值
如何使用 Objective C 从 iOS 中的 Core Data 中过滤数据