我尝试使用Xamarin在Visual Studio中创建带有聊天功能的Android / IOS应用。而且我对聊天气球有疑问。对于Android,可以,我将“文本视图”用作消息容器。但是在IOS中,我遇到了一个问题-如果使用TextView,我只会得到白色矩形。为了实现消息列表,我将UITableView与以下代码一起使用
public override void ViewDidLoad(){
_chatsMessagesListAdapter = new MessagesTableSourceClass(chatData._messages);
messagesList.Source = _chatsMessagesListAdapter;
}
public class MessagesTableSourceClass : UITableViewSource
{
readonly List<MessageData> _dataList;
public MessagesTableSourceClass(List<MessageData> dataList)
{
_dataList = dataList;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return _dataList.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
OwnerMessageTableViewCell cell = tableView.DequeueReusableCell(MessageTableViewCell.Key) as OwnerMessageTableViewCell ?? OwnerMessageTableViewCell.Create();
cell.BindData(_dataList[indexPath.Row]._message, _dataList[indexPath.Row]._image);
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
}
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
return new UIView();
}
}
OwnerMessageTableViewCell.xib是简单结构TableViewCell->视图-> TextView
但是结果是TableView messagesList显示为白色矩形。
有人可以帮我解决这个问题吗?
更新
在这里实现OwnerMessageTableViewCell
公共局部类OwnerMessageTableViewCell:UITableViewCell
{
公共静态只读UINib Nib = UINib.FromName(“ OwnerMessageTableViewCell”,NSBundle.MainBundle);
公共静态只读NSString Key = new NSString(“ OwnerMessageTableViewCell”);
公共OwnerMessageTableViewCell(IntPtr句柄):基本(句柄)
{
}
公共静态OwnerMessageTableViewCell Create()
{
返回(OwnerMessageTableViewCell)Nib.Instantiate(null,null)[0];
}
内部void BindData(字符串消息,UIImage头像)
{
messageIcon.Image =头像;
workerIcon.Hidden = false;
messageText.Text =消息;
}
}
这里xib文件内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="252" customClass="OwnerMessageTableViewCell" rowHeight="44">
<rect key="frame" x="0.0" y="0.0" width="240" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="252" id="253">
<rect key="frame" x="0.0" y="0.0" width="240" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="280" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" clipsSubviews="YES" image="Images/worker_icon.png">
<rect key="frame" x="190" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute keyPath="layer.cornerRadius" type="number">
<real key="value" value="25"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="justified" id="307" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" text="Message Text here..." editable="NO" selectable="NO">
<rect key="frame" x="0.0" y="0.0" width="182" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" colorSpace="calibratedRGB" red="0.094117647058823528" green="0.31372549019607843" blue="0.63921568627450975" alpha="1"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<inset key="scrollIndicatorInsets" minX="5" minY="5" maxX="5" maxY="5"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute keyPath="layer.cornerRadius" type="number">
<real key="value" value="15"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="384" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" image="Images/worker_icon.png">
<rect key="frame" x="220" y="30" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
</tableViewCellContentView>
<point key="canvasLocation" x="-132.5" y="-317"/>
<connections>
<outlet property="messageIcon" destination="280" id="name-outlet-280"/>
<outlet property="messageText" destination="307" id="name-outlet-307"/>
<outlet property="workerIcon" destination="384" id="name-outlet-384"/>
</connections>
<color key="backgroundColor" colorSpace="calibratedRGB" red="0.54117647058823526" green="0.54117647058823526" blue="0.54117647058823526" alpha="1"/>
</tableViewCell>
</objects>
<resources>
<image name="chat_bg.jpg" width="414" height="648"/>
</resources>
</document>
更新
这是VS设计器窗口的屏幕
;
;
;
最佳答案
您需要根据文本调整单元格的高度。
本机ios中有几种解决方案,建议您查看Auto-Sizing Row Height。
脚步:
修改Content Hugging Priority
上的Content Compression Resistance Priority
和UITextView
以操作单元格布局
设置Estimated RowHeight
以启用自动调整大小。